Title: Controls
1Controls
2Create this Example CalledStrings
HscrollBox
RadioButtons in a Group
CheckBoxes
ListBox
3The CheckBox Control (chk)
- Allows the user to select a yes / no response
- Also allows the user to select from a third
choice called indeterminate - Indeterminate usually indicates that no choice
has been made - CheckBox displays textual description along with
a box that can be checked or unchecked (or
indeterminate)
4CheckBox Control (Properties)
- CheckAlign - controls position of box within the
visible region of the control instance - CheckState - defines whether the box is checked,
not checked, or indeterminate - Text - defines the text that is appearing in the
visible region
5CheckBox Control (Events)
- CheckedChanged event fires whenever value of
Checked property changes - Example Setting the CheckState property
- chkFemale.CheckState CheckState.Checked
- chkMale.CheckState CheckState.Unchecked
- chkFemale.CheckState CheckState.Indeterminate
6ScrollBar Controls
- Used to select a value from a range of values
- Often used to represent a percentage
- Values are always integers
- Two controls work the same way
- HScrollBar appears horizontally
- Standard prefix is "hsb"
- VScrollBar appears vertically
- Standard prefix is "vsb"
- Value grows as scroll bar is moved downward
7ScrollBar Controls (Properties and Events)
- Properties
- Maximum and Minimum - defines the valid range
- Default values are 100 and 0 respectively
- Value - contains current value between the
defined range - Value must be between Minimum and Maximum
- SmallChange and LargeChange - controls amount of
change to Value property as user clicks control
instance region - Events
- ValueChanged event fires when user changes value
property.
8ScrollBar Behavior
Click here to change the Value property by
SmallChange
Click here to change the Value property by
LargeChange
Relative indicator of Value Property
9ScrollBar (ValueChanged Example)
- Store the scroll bar's value into the Label named
lblFuel when the value changes - Private Sub hsbAge_ValueChanged(. . .)
- lblAge.Text hsbAge.Value.ToString
- End Sub
10The NumericUpDown Control
- Similar to scroll bars
- Displays a value within a range of values
- Displays an adjacent label containing the current
value - Supports same fundamental properties as scroll
bars - Maximum, Minimum, Value properties
- ValueChanged event
- Standard prefix is "nud"
11The GroupBox Control (grp)
- Acts as a container for other control instances
such as RadioButton control instances - Other control instances can be grouped in a
GroupBox too - Properties
- BackColor and ForeColor define colors
- Text property defines text appearing at the top
of the visible region - Replaces the Frame control found in VB 6
12The RadioButton Control
- User selects one radio button from a group of
buttons - called a button group
- Button group contained by a GroupBox control
instance - To create multiple button groups, create multiple
GroupBox control instances
13The RadioButton Control (Caveats)
- Be careful when creating a button group
- Create RadioButtons inside of a group box
- Test by moving group box. Contained radio buttons
should move with the group box - If copying and pasting radio buttons, select
GroupBox before pasting radio button
14RadioButton Control (Properties and Events)
- Properties
- Text - contains text appearing next to radio
button - Checked - indicates whether radio button is
checked or not - Events
- CheckedChanged event fires when user clicks radio
button - Standard prefix is "rad"
15List Boxes and ComboBoxes (continued)
- ListBox control (lst)
- Simple List Box with/without scroll bars
- ComboBox control (cbo)
- List may allow for user to add new items
- List may "drop down" to display items in list
16Various Styles of List Boxes and Combo Boxes
Dropdown Combo Box
Simple Combo Box
List Boxes
Dropdown List Box
17The Items Collection
- List of items in a ListBox or ComboBox is a
collection - Collections are objects that have properties and
methods that allow you to - Add items
- Remove items
- Refer to individual elements
- Count items
- Clear the collection
18Filling a List
- Design time in Properties window
- Items property
- Click on ellipses to open StringCollection
Editor - Type list items, end each line withENTER key
- Run time methods
- Items.Add OR
- Items.Insert
19Filling a List - Design Time
Click ellipses button to open
20Items.Add Method
- Use to add new items to the list at run time
- General Form
-
- Examples
Object.Items.Add(ItemValue)
schoolsListBox.Items.Add("Harvard") schoolsListBox
.Items.Add("Stanford") schoolsListBox.Items.Add(sc
hoolsTextBox.Text) majorsComboBox.Items.Add(majors
ComboBox.Text) majorsComboBox.Items.Add(majorStrin
g)
21Items.Insert Method
- Use to add new items to the list at run time in a
specific location (index) in the collection - General Form
- Examples
Object.Items.Insert(IndexPosition, ItemValue)
schoolsListBox.Items.Insert(0, "Harvard") majorsCo
mboBox.Items.Insert(1, majorsComboBox.Text)
22The SelectedIndex Property
- Index number of currently selected item is stored
in the SelectedIndex property - If no list item is selected, SelectedIndex
property is negative 1 (-1) - Use to select an item in list or deselect all
items in code
23The SelectedItem Property
- If listbox.SelectItem "" Then
-
- Or
- If Not listbox.SelectItem Is Nothing Then
-
24The Items.Count Property
- Use to determine number of items in the list
Remember Items.Count is always one more than
the highest possible Selected Index because
indexes begin with 0 For example, if there are
five items in a list Items.Count
5 AND Highest Index 4
25Referencing the Items Collection
- Use the index of the item to reference a specific
item in the collection - Remember that the index is zero based so the
first item in the list is index position zero
schoolsListBox.Items(5) "University of
California" ' Next line references the currently
selected item. selectedFlavorString
flavorListBox.Items(flavorListBox.Selected
Index).ToString( )
26The Items.RemoveAt Method
- Use the Items.RemoveAt method to remove an item
by index from the list - General Form
- Examples
Object.Items.RemoveAt(IndexPosition)
namesListBox.Items.RemoveAt(0) schoolsComboBox.Ite
ms.RemoveAt(indexInteger) ' Next line removes the
currently selected item. coffeeComboBox.Items.Remo
veAt(coffeeComboBox.SelectedIndex)
27The Items.Remove Method
- Use the Items.Remove method to remove an item by
specifying the text - General Form
- Examples
Object.Items.Remove(TextString)
namesListBox.Items.Remove("My School") schoolsComb
oBox.Items.Remove(schoolTextBox.Text) ' Next line
removes the currently selected item. coffeeComboBo
x.Items.Remove(coffeeComboBox.Text)
28Clearing a List
- Use the Items.Clear method to clear all items and
empty a combo box or list box - General Form
- Examples
Object.Items.Clear( )
schoolsListBox.Items.Clear( ) majorsComboBox.Items
.Clear( )
29List Box and Combo Box Events
- TextChanged Event
- Occurs when user types text into combo box
- List box does not have TextChanged Event
- SelectedIndexChanged Event
- Enter Event (receive focus)
- Leave Event (lose focus)
30Forms
31Adding a Form to a Solution (1)
- Most applications are made up of multiple forms
- To add a new form, click Project and then click
Add New Item - Select the type of item to add from the dialog
box - To add an existing form, click Project, and then
click Add Existing Item - Select the type of item to add from the dialog box
32Adding a Form to a Solution (2)
Enter form name
33Defining the Startup Object (1)
- Every solution has one startup object
- Startup object can be a form or a sub procedure
named Main - Set the startup object using the Project's
General Property Page - Right-click the project in the Solution Explorer,
and then select Property Pages to activate the
Property Pages
34Defining the Startup Object/Form
35Displaying a Form as a Modal Dialog Box
36Displaying, Closing, and Hiding Forms
- Call the Show method to display a form
- Call the ShowDialog method to display a form as a
modal dialog box - Call the Hide method to hide a form but leave the
form in memory - Call the Close method to close the form and
remove it from memory - TO Close the program with more than 1 form
running - Application.close