lecture vb - program controls
DESCRIPTION
visual basicTRANSCRIPT
![Page 1: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/1.jpg)
Check boxes provide a way to make choices from a list of potential
candidates. Some, all, or none of the choices in a group may be
selected.
Check Box Properties:
Text Identifying text next to box.
Font Sets font type, style, size.
CheckState Indicates if unchecked, checked or
indeterminate
Check Box Events:
Checked Triggered when a box is clicked. Value
property is automatically changed by Visual
Basic.
Radio buttons provide the capability to make a mutually exclusive
choice among a group of potential candidate choices. Hence, option
buttons work as a group, only one of which can have a True (or
selected) value.
Radio Button Properties:
Text Identifying text next to button.
Font Sets font type, style, size.
Checked Indicates if selected (True) or not (False). Only
one option button in a group can be True. One
Check Boxes
Radio Buttons
![Page 2: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/2.jpg)
button in each group of option buttons should
always be initialized to True at design time.
Option Button Events:
Click Triggered when a button is clicked. Value
property is automatically changed by Visual
Basic.
GroupBox provide a way of grouping related controls on a form.
And, in the case of option buttons, frames affect how such buttons
operate.
To group controls in a groupbox, you first draw the frame. Then, the
associated controls must be drawn in the groupbox. This allows you
to move the groupbox and controls together.
Drawing the controls outside the groupbox and dragging them in,
copying them into a groupbox, or drawing the groupbox around
existing controls will not result in a proper grouping. It is perfectly
acceptable to draw groupbox within other groupbox.
As mentioned, groupbox affect how option buttons work. Option
buttons within a frame work as a group, independently of option
buttons in other frames. Option buttons on the form, and not in
groupbox, work as another independent group. That is, the form is
itself a groupbox by default. We'll see this in the next example.
GroupBox
![Page 3: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/3.jpg)
It is important to note that an independent group of option buttons is
defined by physical location within groupbox, not according to naming
convention.
Groupbox Properties:
Text Title information at top of frame.
Font Sets font type, style, size.
Program-Control Statements
Like any programming language, the control statements are the
essence of VB programming language because it governs the flow of
program execution. The way they are implemented in many ways defines
the personality of the VB language.
The program-control statements may be separated into three
categories. The first consists of the conditional instructions if and case.
The second are the loop-control statements such as while, for and do-
while. The final category is the unconditional branch instruction label.
The If Then Statement
If statement executes a statement or a block of codes if an expression is true.
General syntax:If (boolean expression) Then
statementEnd If
where:
![Page 4: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/4.jpg)
Boolean expression is a combination of relational operators, conditional operators, and values resulting in a value of true or false
Statement represents the lines of code that are executed if expression is true
Example:
If (grade >= 75) Then Label2.Text = "Passed" End If
Implementation:
1. Design the following form.
2. Set properties as:
Label 1:
Text: Input Grade
Label 2:Text: [BLANK]
Button1:Text: Evaluate
3. Declare a variable that will hold the value of TextBox1 as:
Dim grade As Short
The program will check if the grade in the TextBox1 is a passing grade once the Button1 is clicked. Double click on Button1 and inside the procedure type:
![Page 5: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/5.jpg)
grade = Val(TextBox1.Text)If (grade >= 75) Then
Label2.Text = "Passed" End If
The If Then Else Statement
If statement executes a statement or a block of codes if an
expression is true, otherwise the else statement will be executed.
General syntax:If (boolean expression) Then
statementElse
statementEnd If
where:
Boolean expression is a combination of relational operators,
conditional operators, and values resulting in a value of true
or false
Statement1 represents the lines of code that are executed if
expression is true and statement2 represents the lines of
code that are executed if expression is false.
Example:
If (grade >= 75) Then Label2.Text = "Passed"Else
Label2.Text = "Failed"End If
Implementation:
Modify the Evaluate Button above and change the code into: grade = Val(TextBox1.Text)
![Page 6: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/6.jpg)
If (grade >= 75) Then Label2.Text = "Passed" Else Label2.Text = "Failed" End If
The If Then ElseIf Statement
Another variation of if statement is if then elseif statements to state
multiple outcomes for several different expressions.
General syntax:If (boolean expression) Then
statement1Else if (boolean expression) Then
statement2Else
statement3
where:
Boolean expression is a combination of relational operators,
conditional operators, and values resulting in a value of true
or false
Statement1 represents the lines of code that are executed if
expression is true. Statement2 represents the lines of code
that are executed if the else if expression is true and
statement3 represents the lines of code that are executed if
both Boolean expressions are false.
Example:
If (grade >= 75 And grade <= 100) Then Label2.Text = "Passed" ElseIf (grade <= 74) Then Label2.Text = "Failed" Else
![Page 7: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/7.jpg)
Label2.Text = "Invalid Input" End If
Implementation:
Modify the program again, this time we will limit the input up to
100, since there is no grade higher than 100.
grade = Val(TextBox1.Text) If (grade >= 75 And grade <= 100) Then Label2.Text = "Passed" ElseIf (grade <= 74) Then Label2.Text = "Failed" Else Label2.Text = "Invalid Input" End If
The Select Case Statement
Another control statement that runs one of several groups of
statements, depending on the value of an expression.
General syntax:Select Case TestExpression
Case ExpressionList1
Statement1
Case ExpressionList2
Statement1
Case Else
Statement3
End Select
where:
TestExpression is the value to be compared to the case
statements
![Page 8: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/8.jpg)
Statement1 represents the lines of code that are executed if
expression is ExpressionList1 is true. Statement2 represents
the lines of code that are executed if the else if
ExpressionList2 is true and statement3 represents the lines of
code that are executed if both Boolean expressions are false.
Example:
Select Case grade Case 0 To 74 Label2.Text = "Failed" Case 75 To 100 Label2.Text = "Passed" Case Else Label2.Text = "Invalid Input" End Select
Implementation:
Let’s convert the last example to the Select Case Statement by
changing the code now into:
grade = Val(TextBox1.Text) Select Case grade Case 0 To 74 Label2.Text = "Failed" Case 75 To 100 Label2.Text = "Passed" Case Else Label2.Text = "Invalid Input" End Select
A list box displays a list of items from which the user can select one
or more items. If the number of items exceeds the number that can
be displayed, a scroll bar is automatically added.
![Page 9: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/9.jpg)
Some of the List Box Properties:
HorizontalScrollbar: Displays a horizontal scrollbar to the
ListBox.
MultiColumn: Works when the ListBox has
MultipleColumns.
The default value is set to False. Set it to
True if you want the list box to display
multiple columns.
ScrollAlwaysVisible: Default value is set to False. Setting it to
True will display both Vertical and
Horizontal scrollbar always.
SelectionMode: Default value is set to one. Select option
None if you do not any item to be selected.
Select it to MultiSimple if you want multiple
items to be selected. Setting it to
MultiExtended allows you to select multiple
items with the help of Shift, Control and
arrow keys on the keyboard.
Sorted: Default value is set to False. Set it to True if
you want the items displayed in the ListBox
to be sorted by alphabetical order.
Working with ListBoxes
![Page 10: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/10.jpg)
To Add Contents to the ListBox
Contents in the ListBox can be added in two ways such as:
1. To the Properties Window, choose Items and you can directly supply the contents of your ListBox
2. To your program, using the ListBox.Items.Add (“Items to be Added”)
To Refer Items in the ListBox
Items in a ListBox are referred by index. When items are added to the ListBox they are assigned an index. The first item in the ListBox always has an index of 0 the next 1 and so on.
To Display the Index of an Item
TextBox1.Text = ListBox1.SelectedIndex'this line of code will display the index number of the selected item from the List in a TextBox
To Count the Number of Items in a ListBox
TextBox1.Text = ListBox1.Items.Count'this line will count the number of items in the ListBox and displays in a TextBox
To Display the Item Selected from ListBox in a TextBox
TextBox1.Text = ListBox1.SelectedItem'using this line will display the selected item in a TextBox
To Remove Items from a ListBox
You can remove all items or one particular item from the list box by
calling the Remove or RemoveAt method to delete items. Remove has
![Page 11: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/11.jpg)
one argument that specifies the item to remove. RemoveAt removes the
item with the specified index number.
ListBox1.Items.RemoveAt(4)
'this removes an item at specific index 4 from the ListBox
ListBox1.Items.Remove(ListBox1.SelectedIndex)
‘this removes the selected item from the List Box
To Remove All Items
ListBox1.Items.Clear( )'using this line will clear or empty the ListBox
Example:1. Create a new Windows application and draw the following controls:
The combo box is similar to the list box. The differences are a combo box includes a text box on top of a list box and only allows selection of one item. In some cases, the user can type in an alternate response.
Butto
Butto
Butto
Butto
ListBo
Combo Boxes
![Page 12: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/12.jpg)
DropDownStyle The default value is set to DropDown which means
that the ComboBox displays the Text set by it's
Text property in the Textbox and displays it's
items in the DropDownListBox below. Setting it to
simple makes the ComboBox to be displayed with
a TextBox and the list box which doesn't drop
down. Setting it to DropDownList makes the
ComboBox to make selection only from the drop
down list and restricts you from entering any text
in the textbox.
Sorted We can sort the ComboBox with this property
which is set to False by Default.
Items We can add items to the ComboBox with this
property.
ComboBox Event
The default event of ComboBox is SelectedIndexChanged which
looks like this in code:
Working with ComboBoxes
To display the selection made in the ComboBox in the Textbox the code looks like this:
To Add Contents to the ComboBox
![Page 13: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/13.jpg)
Contents in the ComboBox can be added in two ways such as:
1. To the Properties Window, choose Items and you can directly
supply the contents of your ComboBox
2. To your program, using the ComboBox.Items.Add (“Items to be
Added”)
To Refer Items in the ComboBox
Items in a ComboBox are referred by index. When items are
added to the ComboBox they are assigned an index. The first item in
the ComboBox always has an index of 0 the next 1 and so on.
To Display the Index of an Item
TextBox1.Text = ComboBox1.SelectedIndex
'this line of code will display the index number of the
selected item from the ComboBox in a TextBox
To Count the Number of Items in a ComboBox
TextBox1.Text = ComboBox1.Items.Count
'this line will count the number of items in the
ComboBox and displays in a TextBox
To Display the Item Selected from ComboBox in a TextBox
TextBox1.Text = ComboBox1.SelectedItem
'using this line will display the selected item in a
TextBox
![Page 14: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/14.jpg)
To Remove Items from a ComboBox
You can remove all items or one particular item from the list box
by calling the Remove or RemoveAt method to delete items. Remove
has one argument that specifies the item to remove. RemoveAt
removes the item with the specified index number.
ComboBox1.Items.RemoveAt(4)
'this removes an item at specific index 4 from the
ComboBox
ComboBox1.Items.Remove(ListBox1.SelectedIndex)
‘this removes the selected item from the Combo Box
To Remove All Items
ComboBox1.Items.Clear( )
'using this line will clear or empty the ComboBox
To Access a Specific Item
Accessing specific items in a Windows Forms combo box, list box,
or checked list box is an essential task. It enables you to
programmatically determine what is in a list, at any given position. You
can do this by using the GetItemText.
Example:
1. Create a new Windows form and design the following form:
![Page 15: Lecture VB - Program Controls](https://reader035.vdocument.in/reader035/viewer/2022062305/5695d40c1a28ab9b02a014df/html5/thumbnails/15.jpg)
ComboBox1
Label2
Label1