How to Create a 'What Should I Do?' Program in Visual Basic
Submitted by Yorkiebar on Thursday, October 17, 2013 - 05:43.
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.
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).
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.
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.
Project Complete!
Below is the full source code and download to the project files.
- Dim options As New List(Of String)
- Dim rand As Random = New Random()
- If (Not TextBox1.Text = Nothing And isNewOption(TextBox1.Text)) Then
- options.Add(TextBox1.Text)
- TextBox1.Text = Nothing
- End If
- Private Function isNewOption(ByVal s As String)
- For Each o As String In options
- If (o.ToLower() = s.ToLower()) Then Return False
- Next
- Return True
- End Function
- If (options.Count() > 0) Then
- Dim r As Integer = rand.Next(options.Count())
- MsgBox("You should " & options(r))
- End If
- Public Class Form1
- Dim options As New List(Of String)
- Dim rand As Random = New Random()
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- If (Not TextBox1.Text = Nothing And isNewOption(TextBox1.Text)) Then
- options.Add(TextBox1.Text)
- TextBox1.Text = Nothing
- End If
- End Sub
- Private Function isNewOption(ByVal s As String)
- For Each o As String In options
- If (o.ToLower() = s.ToLower()) Then Return False
- Next
- Return True
- End Function
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- If (options.Count() > 0) Then
- Dim r As Integer = rand.Next(options.Count())
- MsgBox("You should " & options(r))
- End If
- End Sub
- 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
- 65 views