Motivational Quote Selector in Visual Basic

Introduction: This tutorial is on how to create a motivational quote selector to show you one random quote every day that will get you thinking. The Website: First we need a source of the random quotes, I've found this website which has an entire list of good, motivational quotes with the author listed as well... http://www.forbes.com/sites/kevinkruse/2013/05/28/inspirational-quotes/ Design: Next we need the design for our program... well, actually there is no design. In fact, all we need is a form load sub so that it can load the program, get a quote, display the quote, and close the application. Form Load... So, on the form load, as stated above, we need to retrieve and display a random quote. Before we can begin, we want to import the following namespaces and create a couple of functions...
  1. Imports System.Net, System.IO, System.Text.RegularExpressions
  1. Private Function GetBetweenAll(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String) As String()
  2. Dim Results, T As New List(Of String)
  3. T.AddRange(Regex.Split(Source, Str1))
  4. T.RemoveAt(0)
  5. For Each I As String In T
  6. Results.Add(Regex.Split(I, Str2)(0))
  7. Next
  8. Return Results.ToArray
  9. End Function
  10.  
  11. Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
  12. Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0)
  13. End Function
The getBetween function simply returns a string from the containing string between the two string points. While the getBetweenAll function does it collectively.. So first we get the web page source from the forbes website listed above in the 'The Website' section of this article...
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2. Dim req As HttpWebRequest = HttpWebRequest.Create("http://www.forbes.com/sites/kevinkruse/2013/05/28/inspirational-quotes/")
  3. Dim res As HttpWebResponse = req.GetResponse()
  4. Dim src As String = New StreamReader(res.GetResponseStream()).ReadToEnd()
We send a request to the site, catch the response and read it using a new System.IO.StreamReader to a new string variable named 'src', standing for 'Source'. Next we extract the containing '
    ' tags of the '
  1. ' tags containing the quotes...
    1. Dim container As String = getbetween(src, "<ol>", "</ol>")
    Then we extract each '
  2. ' tag (quote text) to a list of strings named 'quotes'...
    1. Dim quotes As String() = getbetweenall(container, "<li>", "</li>")
    Finally we select a random quote from the list using a Random variable data type class, parse it through a new function named 'replaceHTMLChars' and output the resulting quote in a message box...
    1. Dim random As Random = New Random()
    2. MsgBox(replaceHTMLChars(quotes(random.Next(quotes.Count))))
    Finally we close the form application and end the form1 load sub...
    1. Me.Close()
    2. End Sub
    Replacement Function: The 'replaceHTMLChars' function simply takes a string, replaces the HTML strings (such as £) with plain text characters (£ = £) and returns the converted string...
    1. Private Function replaceHTMLChars(ByVal quote As String)
    2. Dim ret As String = quote
    3. If (ret.Contains("&nbsp;")) Then ret.Replace("&nbsp;", vbTab)
    4. If (ret.Contains("&dash;")) Then ret.Replace("&dash;", "-")
    5. If (ret.Contains("&pound;")) Then ret.Replace("&pound;", "£")
    6. Return ret
    7. End Function
    Scheduling The Application: Next we want to schedule the application to run at a certain time each day/week. First go to Start Menu > Search > Schedule Tasks (on Windows 8). Click on 'Create Basic Task...', set the title to 'Quote' and description to 'Displays a random quote daily/weekly.' then click on the 'Trigger' tab/'Next' button. Select daily or weekly (or any other option, logged in maybe), click 'Next'. Set the appropriate start date and time. Click 'Next' again. Select 'Start a Program' then click 'Browse...' and select your application - it should be in your Visual Basic workspace > Projects > your project file > bin > debug/release. If it is not there, go back in to Visual Studio and go to 'Build' > 'Build {project name}'. Click 'Next' followed by 'Finish'. Done!

Comments

Add new comment