CS0004: Introduction to Programming Select Case Statements and Selection Input
Review • A condition is either… • True or False • Relational Operators • = • <> • < • > • <= • >= • Logical Operators • And • Or • Not
Select Case Statements • Sometimes you want to check if a variable is one of many values. You can do this with If statements: Dim a As Integer If a = 1 Then ‘do something ElseIf a = 2 Then ‘do something ElseIf a = 3 Then ‘do something Else ‘do something • However, you can use select case statements, instead to form a more logical decision structure Dim a As Integer Select Case a Case 1 ‘do something Case 2 ‘do something Case 3 ‘do something Case Else ‘do something End Select
Select Case Statements • General Form Select Case selector Case valueList1 action1 Case valueList2 action2 Case Else actionOfLastResort End Select • The value lists can be literals, variables, expressions, or a range in the form a To b where a and b are literals, variables, or expressions. • Value lists can also be multiple values separated by a comma, for instance you can have: Case 1, 2, 5 • This case will be executed if the selector is either 1, 2 or 5
Select Case Examples • New Topics: • Select Case Statements • Ranges
Selection Input • Some controls in VB ask the user to make selections from multiple choices. Some Examples are: • Radio Buttons • Check Boxes • List Boxes • Radio buttons and check boxes can be grouped together using group boxes. • If controls are inside of a group box, it is said that the controls are attached to the group box. • If multiple radio buttons are in the same group box, only one of them can be selected at a time.
Radio Buttons • Radio buttons are controls that when grouped, only one can be checked (selected) at a time. • Radio buttons default event procedure is CheckedChanged • This event happens when a radio button is either first checked or unchecked. • Radio buttons have a property called Checked that holds the boolean value of whether the box is checked or not. radButton.Checked • You can set whether a radio button is checked or not checked with this property radButton.Checked = True radButton.Checked = False
Select Case Statement with Radio Buttons Example • New Topics: • Radio Buttons • CheckChanged Event Procedure • Checked Property
List Boxes • An input box is a list of strings in which each can be selected. • We have added items to the list and cleared the list in previous examples lstTheList.Items.Add(parameter) lstTheList.Items.Clear() • You can also add items to the list box at design time in Visual Studio. • Click the task button (the triangle) on the control in Visual Studio. • Click on “Edit Items” on the menu that appears • Add each item in a single line in the dialog box. • Click “OK” • You can access the item currently selected by using the Text property of the list box. • The list box default event procedure is SelectedIndexChanged. This means that the item that is selected has changed.
List Box Example • New Topics: • List Box • Design time list items
Check Boxes • Check boxes are like radio buttons, but multiple check boxes can be checked at once. • They also have a checked property chkTheCheckBox.Checked • The default event procedure for a check box is CheckChanged
Check Box Example • New Topics: • Check Boxes • Font Property • Font Class • New Keyword