The remaining control types in this chapter's sample program form are discussed in earlier programs. So let's run the program and then walk through its code. A sample run is shown in Figure 20.7.
Figure 20.7. A sample run of the controls test program.
The program accepts the customer's name , the type of sandwich the customer wants, the bread type and whether the bread is toasted, and a list of condiments to put on the sandwich. When the user clicks the Check Order button, the program builds an order string that the user could read back to the customer just to make sure everything is correct before sending the order to the kitchen via the Submit Order button.
The code for the program is presented in Listing 20.1.
Listing 20.1 Code for the Test Controls Program
Public Class frmControlTest Inherits System.Windows.Forms.Form ' Windows Form Designer generated code Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles MyBase.Load cmbSandwich.Items.Add("Hamburger") ' Sandwich types cmbSandwich.Items.Add("Breaded Tenderloin") cmbSandwich.Items.Add("Sub") cmbSandwich.Items.Add("Liverwurst") cmbSandwich.Items.Add("Ham") cmbSandwich.Items.Add("Turkey") cmbSandwich.Items.Add("Chicken") cmbSandwich.Items.Add("Aunt Nancy's Special") cmbSandwich.Items.Add("Joyce's Jumbo Special") cmbSandwich.SelectedIndex = 0 lsbCondiments.Items.Add("Yellow Mustard") ' Condiments lsbCondiments.Items.Add("Honey Mustard") lsbCondiments.Items.Add("Grey Poupon") lsbCondiments.Items.Add("Onions") lsbCondiments.Items.Add("Relish") lsbCondiments.Items.Add("Peppers") lsbCondiments.Items.Add("Lettuce") lsbCondiments.Items.Add("Olives") lsbCondiments.Items.Add("Mayo") lsbCondiments.Items.Add("Tomatoes") lsbCondiments.Items.Add("American Cheese") lsbCondiments.Items.Add("Swiss Cheese") lsbCondiments.Items.Add("Salsa") lsbCondiments.Items.Add("Dump The Garden") lsbBreadType.Items.Add("White Bread Bun") ' Break types lsbBreadType.Items.Add("Rye Bread Bun") lsbBreadType.Items.Add("Kaiser Roll") lsbBreadType.Items.Add("Bagel") lsbBreadType.Items.Add("Sour Dough") lsbBreadType.Items.Add("French Bread") lsbBreadType.SelectedIndex = 0 rbnToasted.Checked = True lsbBreadType.Cursor = Cursors.Hand End Sub Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnCheck.Click Dim i As Integer Dim LastOne As String Dim buff As String txtOrder.Clear() ' Clear out old order ' Build the new order txtOrder.AppendText(txtCustomer.Text) ' Customer name txtOrder.AppendText(" ordered a ") txtOrder.AppendText(cmbSandwich.SelectedItem) ' the sandwich txtOrder.AppendText(" on a ") If rbnToasted.Checked = True Then ' toasted? LastOne = " toasted " Else LastOne = " plain " End If txtOrder.AppendText(LastOne) txtOrder.AppendText(lsbBreadType.SelectedItem) ' Bread type txtOrder.AppendText(" with ") For i = 0 To lsbCondiments.Items.Count - 1 ' Condiments? If lsbCondiments.GetSelected(i) = True Then LastOne = lsbCondiments.Items(i) buff &= LastOne & ", " End If Next i = InStr(buff, LastOne) ' Change trailing comma to a period If (i) Then buff = Microsoft.VisualBasic.Left(buff, i - 1) & " and " & LastOne & _ "." End If txtOrder.AppendText(buff) ' Display the order End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnExit.Click Me.Dispose() End Sub Private Sub txtCustomer_Leave(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles txtCustomer.Leave cmbSandwich.DroppedDown = True ' Show all the options Cursor.Current = Cursors.Arrow ' Set the current cursor type End Sub End Class
The following sections describe the important parts of this code.
The Form's Load() Event
The bulk of the code in the form's Load() event in Listing 20.1 deals with adding the combo box and list box options. Both the ComboBox and ListBox controls use the Add() method to add the list of options to the Items collection. Near the bottom of the procedure, you set the Toasted radio button to be checked because that is to be the default selection.
Just for grins, you set the Cursor property for the bread type ListBox control to be the hand cursor of the Cursors class. When the user places the arrow cursor over the lstBreadType list box, the cursor changes to a hand cursor. Moving off the list box automatically restores the arrow cursor. There's no real reason for changing the cursor other than to show you how it can be done.
The btnCheck Object's Click() Event
Visual Basic .NET automatically handles the code to process the selections made by the user. That is, clicking a selection in either the list box or combo box automatically highlights the selection. Therefore, your task is to build a completed order string from the selections made by the user.
The call to the txtOrder.Clear() method simply clears out any previous order than might be showing. That call is followed by a series of txtOrder.AppendText() method calls. Using the AppendText() method is another way of appending text to the contents of a text box. Although the string concatenation operator ( & ) works just fine with text boxes, the AppendText() method is considerably faster. In most programs, this speed improvement isn't noticeable. However, if you built a long (multiline) text box string in a tight loop, the efficiency gain might be noticeable.
This statement shows you how to retrieve the string of a selected item from a combo box:
txtOrder.AppendText(cmbSandwich.SelectedItem) ' the sandwich
If you look about eight lines further down in Listing 20.1, you can see that selecting an item from a list box uses the same syntax structure.
Multiple Selections in a List Box
The lsbCondiments list box code is a little different from the code for processing the lsbBreadType list box. The lsbBreadType object's SelectionMode property is set to One , which means that only one item can be selected from the list. If the user changes his or her selection, Visual Basic .NET automatically highlights the new selection and removes the highlight from the old selection. You do not have to write code to manage this feature.
You set the SelectionMode property in the Properties window for the lsbCondiments list box to MultiSimple . This allows the user to select multiple options from the list. Customers might want to add more than one condiment to a sandwich, and setting the SelectionMode property to MultiSimple allows for multiple condiments.
You use the GetSelected() method to determine whether an item in the list box has been selected. If the item is selected, the GetSelected() method returns logic True ; otherwise , it returns logic False . This statement is executed if the call to the GetSelected() method returns a selected item:
LastOne = lsbCondiments.Items(i)
This statement then assigns the string associated with the selected item into LastOne . (You do this assignment because of some postloop processing you want to perform after all the selections are processed .) The list of items is then built as a comma-separated list and stored in a temporary string named buff .
The call to InStr() searches buff to find the last item added to the list of condiments. The call to the Left() function adds the word and to the condiment list before the last selected option and replaces the trailing comma with a period. The only purpose of all this postloop processing is to make the resultant order more readable.
After the order string is built, it is copied into the txtOrder text box for display.
The Leave() event procedure of the txtCustomer text box sets the DroppedDown property of the cmbSandwich combo box to logic True . As mentioned earlier in this chapter, this automatically displays the options in the combo box. You set the current cursor to the default arrow cursor to avoid not showing a cursor to the user (which is the default behavior).
You have not implemented any action for the Submit Order button. In a real application, pressing that button would transmit the order to the kitchen so that the sandwich could be made.
You should save this project and then try changing some of the properties for various controls to observe the impact of your changes. You should also add some new items to the menus and use the online help to see if any other properties and methods might prove useful.