How to Create a 'What Should I Do?' Program in Visual Basic

Language
Introduction: Welcome to my tutorial on how to create a 'What Should I Do?' program to make decisions for you in Visual Basic. Steps of Creation: Step 1: First we want to create a form with; textbox1 - contain new options to add, button1 - add new option contained in textbox1, button2 - choose which option to do! Lets also create a list of string which will contain our options. We also need a random variable to randomly select an option later.
  1. Dim options As New List(Of String)
  2. Dim rand As Random = New Random()
Step 2: On button1 click event (add option) we want to add the textbox1.text option to our options list if it is not nothing and is a new option (not already added to our options list).
  1. If (Not TextBox1.Text = Nothing And isNewOption(TextBox1.Text)) Then
  2.     options.Add(TextBox1.Text)
  3.     TextBox1.Text = Nothing
  4. End If
As you can see, we are using a custom function to check if it is a new option or not, we will create this next. Step 3: So, for our isNewOption function we just want to run through all previously added options and return false if they are the same, if it is not found it will return true as it is a new option.
  1. Private Function isNewOption(ByVal s As String)
  2.     For Each o As String In options
  3.         If (o.ToLower() = s.ToLower()) Then Return False
  4.     Next
  5.     Return True
  6. End Function
Step 4: Finally, we want to do our button2 click event action. First we get a random number higher than -1 and less than the options list count/total, we then output the string in the options list of the index of the randomly generated integer.
  1. If (options.Count() > 0) Then
  2.     Dim r As Integer = rand.Next(options.Count())
  3.     MsgBox("You should " & options(r))
  4. End If
Project Complete! Below is the full source code and download to the project files.
  1. Public Class Form1
  2.     Dim options As New List(Of String)
  3.     Dim rand As Random = New Random()
  4.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5.         If (Not TextBox1.Text = Nothing And isNewOption(TextBox1.Text)) Then
  6.             options.Add(TextBox1.Text)
  7.             TextBox1.Text = Nothing
  8.         End If
  9.     End Sub
  10.  
  11.     Private Function isNewOption(ByVal s As String)
  12.         For Each o As String In options
  13.             If (o.ToLower() = s.ToLower()) Then Return False
  14.         Next
  15.         Return True
  16.     End Function
  17.  
  18.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  19.         If (options.Count() > 0) Then
  20.             Dim r As Integer = rand.Next(options.Count())
  21.             MsgBox("You should " & options(r))
  22.         End If
  23.     End Sub
  24. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment