Visual Basic NET BASICS Lesson 9 Nested If
Visual Basic. NET BASICS Lesson 9 Nested If Statements and Radio Buttons
Objectives n n Use nested If statements. Use radio buttons. Use the form Load event. Use the Select Case statement. 2
Using Nested If Statements n n When you place an If statement inside another If statement, the If statement inside is called a nested If statement. When using nested If statements, it is important that you properly indent the code. 3
Using Radio Buttons n Radio buttons are similar to check boxes with one important difference. ¡ n Radio Buttons appear in groups, and only one button in the group can be selected at a time. To successfully use radio buttons, there are three steps involved: ¡ ¡ ¡ Create a Group. Box to group the radio buttons. Create the radio buttons in the Group. Box. Write code to use the radio buttons. 4
Creating a Group. Box Control n n n A Group. Box control is a container for other controls. The controls that you place inside a Group. Box are treated as one unit. Radio buttons in a Group. Box are sometimes referred to as an option group. 5
Creating Radio Buttons in the Group. Box n n Now that the Group. Box is created, the next step is to add radio buttons to the Group. Box. Like other controls, radio buttons need to be named appropriately using the Name property. 6
Adding Code to the Radio Buttons n n Coding radio buttons requires that you think in an event-driven way. Use form-level variables as the scope to keep track of the option that has been selected. 7
Using a Form Load Event Procedure n n n By setting the Checked property of a radio button to True, the radio button is selected. When a form is loaded and opened by the program, a special event called a Load event is triggered. Like other events, you can write an event procedure for the form’s Load event. 8
Using Select Case n n n In a Select Case statement, you specify a variable to test and then list a number of cases for which you want to test. The first line in a Select Case statement specifies the piece of data that is involved in the decision. You can use conditional operators in a Case statement as well. 9
Setting Additional Properties n n The Back. Color property sets the form’s background color. The Location property allows you to specify the location of an object using X and Y coordinates. 10
Summary n n If statements can be nested to make additional decisions within the code of the If statement. It is important to indent the code in a nested If statement to make the code readable. Each If statement within a nested If statement must end with the End If statement. Radio buttons appear in groups. Only one radio button in the group can be selected at a time. Radio buttons are sometimes called option buttons. 11
Summary (continued) n n The first step in creating a group of radio buttons is to create a Group. Box control to contain the radio buttons. The controls within a Group. Box are treated as one unit. The Text property of a Group. Box control specifies the text that appears at the top of the Group. Box. 12
Summary (continued) n n To associate a radio button with a Group. Box, you must click the Radio. Button tool only once and draw the radio button in the Group. Box. If you double-click to create a radio button, it will not associate itself with the Group. Box. The Text property of a radio button specifies the text that appears on the label attached to the radio button. 13
Summary (continued) n n Coding radio buttons involves using formlevel variables that carry values that reflect the selected radio. A form’s Load event procedure is executed each time a form is loaded and opened by the program. 14
Summary (continued) n The Select Case statement allows you to make multi-way selections. The Case statements in a Select Case can test a range or use conditional operators. Conditional operators in a Case statement must include the Is keyword. As a default, the Case Else statement is applied if no other Case is True. 15
- Slides: 15