Start a new project: the WindowsApplication2 project and rename it to HelloWorld2. Do not select the Create Directory For Solution check box; we’ll save the project from within the IDE. As soon as the project is created, open the File menu and choose Save All to save the project. When the Save Project dialog box appears, click the Browse button to select the folder where the project will be saved. In the Project Location dialog box that appears, select an existing folder or create a new folder such as MyProjects or VB.NET Exercises.
Open the Toolbox and double-click the icon of the ComboBox tool. A ComboBox control will be placed on your form. Now place a Button control on the form and position it so that your form looks like the one shown in Figure 1.10. Then set the button’s Text property to Evaluate My Selection.
We must now populate the ComboBox control with the valid choices. Select the ComboBox control on the form by clicking it with the mouse and locate its Items property in the Properties window. The setting of this property is Collection, which means that the Items property doesn’t have a single value; it’s a collection of items (strings, in this case). Click the ellipsis button and you’ll see the String Collection Editor dialog box, as shown in Figure 1.11.
The main pane in the String Collection Editor dialog box is a TextBox, in which you can enter the items you want to appear in the ComboBox control at runtime. Enter the following strings, one per row and in the order shown here:
- Visual Basic
Click the OK button to close the dialog box. The items will not appear on the control at design time, but you will see them when you run the project. Before running the project, set one more property. Locate the ComboBox control’s Text property and set it to “Select your favorite programming language”. This is not an item of the list; it’s the string that will initially appear on the control.
You can run the project now and see how the ComboBox control behaves. Press F5 and wait a few seconds. The project will be compiled, and you’ll see its form on your Desktop, on top of the Visual Studio window. I’m sure you know how the ComboBox control behaves in a typical Windows application, and our sample application is no exception. You can select an item on the control, either with the mouse or with the keyboard. Click the button with the arrow to expand the list and then select an item with the mouse. Or press the down or up arrow keys to scroll through the list of items. The control isn’t expanded, but each time you click an arrow button, the next or previous item in the list appears on the control. Press the Tab key to move the focus to the Button control and press the spacebar to emulate a Click event (or simply click the Button control).
We haven’t told the application what to do when the button is clicked, so let’s go back and add some code to the project. Stop the application by clicking the Stop button on the toolbar (the solid black square) or by choosing Debug Stop Debugging from the main menu. When the form appears in design mode, double-click the button, and the code window will open, displaying an empty Click event handler. Insert the statements shown in Listing 1.2 between the Private Sub and End Sub statements.
Listing 1.2: The Revised Click Event Handler
Public Class frmSelection Private Sub bttnEvaluate_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles bttnEvaluate.Click Dim selection As String selection = cmbChoice.Text If selection = "Visual Basic" Then MsgBox("Good Choice!") Else MsgBox(selection + " isn't a bad language") End If End Sub End ClassCode language: VB.NET (vbnet)
When the form is first displayed, a string that doesn’t correspond to a language is displayed in the ComboBox control. We can preselect one of the items from within our code when the form is first loaded. When a form is loaded, the Load event of the Form object is raised. Double-click somewhere on the form and the editor will open the form’s Load event handler:
Private Sub Form1 Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load End SubCode language: VB.NET (vbnet)
Enter the following code to select the item Visual Basic when the form is loaded:
Private Sub Form1 Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load cmbChoice.SelectedIndex = 2 End SubCode language: VB.NET (vbnet)
SelectedIndex is a property of the ComboBox control that determines the selected item. You can set it to an integer value from within your code to select an item on the control, and you can also use it to retrieve the index of the selected item in the list. Instead of comparing strings, we can compare the SelectedIndex property to the value that corresponds to the index of the item Visual Basic, with a statement such as the following:
If cmbChoice.SelectedIndex = 2 Then MsgBox("Good Choice!") Else MsgBox(cmbChoice.Text & "isn't a bad language.") End IfCode language: VB.NET (vbnet)
The Text property of the ComboBox control returns the text on the control, and we use it to print the selected language’s name. Of course, if you insert or remove items from the list, you must edit the code accordingly. If you run the application and test it thoroughly, you’ll realize that there’s a problem with the ComboBox control. Users can type a new string in the control, which will be interpreted as a language. By default, the ComboBox control allows users to type in something, in addition to selecting an item from the list. To change the control’s behavior, select it on the form and locate its DisplayStyle property in the Properties window. Expand the list of possible settings for the control and change the property’s value from DropDown to DropDownList. Run the application again and test it; our sample application has become bulletproof. It’s a simple application, but you’ll see more techniques for building robust applications in Chapter 4, ‘‘GUI Design and Event-Driven Programming.’’
The controls on the Toolbox are more than nice pictures we place on our forms. They encapsulate a lot of functionality and expose properties that allow us to adjust their appearance and their functionality. Most properties are usually set at design time, but quite frequently we change the properties of various controls from within our code.
Now that you’re somewhat familiar with the process of building Windows applications, and before you look into any additional examples, I will quickly present the components of the Visual Studio IDE.