Using a CheckBox

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 7.  Windows Forms


A checkbox is used to allow the user to choose an option using a true/false or yes/no type of box. The CheckBox control is similar to the RadioButton control, except that each checkbox can be checked or unchecked individually without affecting another checkbox.

In the program StylesOfGreetings\Step 2 , we will display a standard greeting unless the user checks the personalize button and enters a name . To do this, we made some small changes to the form's appearance. Figure 7-29 illustrates the form's new design-time appearance.

Figure 7-29. Using the CheckBox control to provide options.

graphics/07fig29.jpg

Additional properties that were set in this application are shown in Table 7-5.

Table 7-5. Property Values for the StylesOfGreetings Application (Step 2)
Control Type Name Text Other Properties
Label lblName Name Visible: False
Textbox txtName (blank) Visible: False
CheckBox chkPersonalize Personalize Checked: False

When chkPersonalize is checked, and txtName is visible, the Click Me button uses the name in the greeting. When chkPersonalize is unchecked, lblName and txtName are invisible and the Click Me button uses a standard greeting in the language specified.

To achieve this, we added the line of code indicated below in the btnClickMe's Click event. We also added code to the CheckedChanged event handler for chkPersonalize . The two event handlers for this form are shown below:

 Private Sub btnClickMe_Click(ByVal sender As Object, _  ByVal e As System.EventArgs) Handles btnClickMe.Click    Dim s As String    If rdoFormal.Checked = True Then       s = "Hello "    ElseIf rdoSouthern.Checked = True Then       s = "Howdy "    Else       s = "Hola "    End If    s = s & txtName.Text    MessageBox.Show(s, "Greeting")  txtName.Text = ""  End Sub Private Sub chkPersonalize_CheckedChanged(ByVal sender _  As System.Object, ByVal e As System.EventArgs) _  Handles chkPersonalize.CheckedChanged  lblName.Visible = Not lblName.Visible   txtName.Visible = Not txtName.Visible  End Sub 

Figures 7-30 illustrates the messages that can be displayed both with and without the checkbox checked.

Figure 7-30. Output from CheckBox example.

graphics/07fig30.jpg


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net