- Using underscore "_" to make code more readable by extending the code to the next line
- Message Dialog constant options
- CheckBoxes
- += Addition operator
- The use of OrElse and AndAlso within an If statement
We will use the MessageBox.Show() method and pass a couple constants to help express any problems that might arise in the application. In our case if the user does not enter their name and select at least one movie checkbox then we take an action to help guide the user. We use the MessageBoxButtons.OK and MessageBoxIcon.Error constants in our application. You can use help inside of Visual Studio to discover all of the other options.
The heart of our application revolves around the use of an If statement that uses AndAlso and OrElse conditional operators. Our goal is to make sure our user enters their name AND checks at least one or more movie checkboxes or we display our helpful MessageBox.Show Dialog coaching our user to enter and select the required information before calculating a total cost of the movie selections.
AndAlso is a good way to ensure that two conditions are both true before choosing a path of execution. In our case we make sure a name and at least one checkbox is selected before calculating the total. Our situation is a little more complex becasue we need to account for the selection of one or more checkboxes. The best way to do that is to use the OrElse operator. The OrElse operator ensures that either or both conditions are true before choosing a path of execution.
A simple example of AndAlso is as follows:
If txtName.Text = "" AndAlso age <> 12) OrElse (MovieSelected.Checkbox = True) Then
MessageBox.Show("Your message here...". "Title in Messagebox", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If
We use the Addition operator (+=) again to tally the totals of all the movies and then we use the FormatCurrency() function to convert the string value of the movie to a currency value suitable for the screen.
All of our code in this application is wired to the "Calculate" command button. I don't have time in the videos to debug and perform breakpoints as you should do in real life, so make sure you set breakpoints and step through your code even though it is running as expected. Watching the flow of logic is very helpful in getting this type of stuff comitted to memory for more complex applications in the future.
Here is the source code for the application.
If (txtYourName.Text = "") OrElse _
(chkOldSchool.Checked = False AndAlso _
chkTheRock.Checked = False AndAlso _
chkWeddingCrashers.Checked = False) Then
MessageBox.Show("Please enter your name and check at least one movie", _
"Missing Info", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
' add the price of the movies up
Dim total As Integer
' if user selected Wedding Crashers $9
If chkWeddingCrashers.Checked = True Then
total += 9
End If
' is user selected The Rock $10
If chkTheRock.Checked = True Then
total += 10
End If
' if user selected Old School $12
If chkOldSchool.Checked = True Then
total += 12
End If
' display total and format for currency
txtTotal.Text = FormatCurrency(total)
End If
After reviewing the lesson notes above watch the video lesson. As with all of my lessons the video is produced in HD so be sure to click the HD button in the lower right corner and expand to full screen for the best viewing experience.
If you have any questions or comments, please let me know.
Tim