Lesson 8 Working With Checkbox
Lesson 8 Working With Checkbox
- In this lesson, we shall learn how to write code for the checkbox.
- The Checkbox allows the user to select one or more items by checking the checkbox or
checkboxes concerned.
- For example, in the Font dialog box of any Microsoft Text editor like FrontPage, there are
many checkboxes under the Effects section.
- The user can choose to underline, subscript, small caps, superscript, blink and etc.
- In Visual Basic 2019, you can create a shopping cart where the user can click
on checkboxes that correspond to the items they intend to buy, and the total payment can
be computed at the same time.
- In this example, we add a few labels, two buttons, and six checkboxes.
- We declare the price of each item using the Const keyword.
- If a checkbox is being ticked, its state is True else its state is False.
- To calculate the total amount of purchase, we use the mathematical operator +=. For
example, sum+=BN is actually sum=sum+BN.
- Finally, we use the ToString is a Visual Basic 2019 method to display the amount in
currency.
The Code
Public Class Form1
1
If CheckBox4.Checked = True Then
sum += HD
End If
End Sub
End Class
2
Example 8.2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Const large As Integer = 10.0
Const medium As Integer = 8
Const small As Integer = 5
Dim sum As Integer
Example 8.3
In this example, the text on the label can be formated using the three checkboxes that represent
bold, italic and underline.
The Code
Public Class Form1
End If
End Sub
3
Private Sub ChkItalic_CheckedChanged(sender As Object, e As EventArgs) Handles
ChkItalic.CheckedChanged
If ChkItalic.Checked Then
LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style Or FontStyle.Italic)
Else
LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style And Not FontStyle.Italic)
End If
End Sub
* The above visual basic 2019 program uses the CheckedChanged event to respond to the user
selection by checking a particular checkbox, it is similar to the click event. The statement
will also retain the original font type but change it to regular font style. (The other statements
employ the same logic)
The Output
Figure 8.2