Visual Basic 6.
0 IDE – Integrated Development Environment
Visual Basic 6.0 - Properties, Methods And Events
All the controls in the ToolBox except the Pointer are objects in Visual Basic. These objects have
associated properties, methods and events.
Class box;
Box.a=10
Real world objects are loaded with properties. For example, a flower is loaded certain color, shape
and fragrance. Similarly programming objects are loaded with properties. A property is a named
attribute of a programming object. Properties define the characteristics of an object such as Size,
Color etc. or sometimes the way in which it behaves. For example, a TextBox accepts properties such
as Enabled, Font, MultiLine, Text, Visible, Width, etc.
Enables property allows the TextBox to be enabled or disabled at run time depending on the
condition set to True or False.
Font property sets a particular font in the TextBox.
MultiLine property allows the TextBox to accept and display multiple lines at run time.
Text property of the TextBox control sets a particular text in the control.
Visible property is used to hide the object at run time.
Width property sets the TextBox to the desired width at design time.
The properties that are discussed above are design-time properties that can be set at the design tme
by selecting the Properties Window. But certain properties cannot be set at desgn time. For example,
the CurrentX and CurrentY properties of a Form cannot be set at the design time.
A method is an action that can be performed on objects. For example, a cat is an object. Its properties
might include long white hair, blue eyes, 3 pounds weight etc. A complete definition of cat must only
encompass on its looks, but should also include a complete itemization of its activities. Therefore, a
cat's methods might be move, jump, play, breath etc.
Siimilarly in object-orinted programming, a method is a connected or built-in procedure, a block of
code that can be invoked to impart some action on a particular object. A method requires an object to
provide them with a context. For example, the word Move has no meaning in Visual Basic, but the
statement,
Text1.Move 700, 400
performs a very precise action. The TextBox control has other associated methods such as Refresh,
SetFocus, etc.
textbox1.setfocus
The Refresh method enforces a complete repaint of the control or a Form. For example,
Text1.Refresh refreshes the TextBox.
The Setfocus method moves the focus on the control. For Example Text1.SetFocus sets the
focus to TextBox control Text1.
Event Driven Programming
Visual Basic programs are built around events. Events are various things that can happen in a
program. this will become clearer when studied in contrast to procedural programming. In procedural
languages, an application is written is executed by checking for the program logically through the
program statements, one after another. For a temporary phase, the control may be transferred to
some other point in a program. While in an event driven application, the program statements are
executed only when a particular event calls a specific part of the code that is assigned to the event.
Let us consider a TextBox control and a few of its associated events to understand the concept of
event driven programming. The TextBox control supports various events such as Change, Click,
MouseMove and many more that will be listed in the Properties dropdown list in the code window for
the TextBox control. We will look into a few of them as given below.
The code entered in the Change event fires when there is a change in the contents of the
TextBox
The Click event fires when the TextBox control is clicked.
The MouseMove event fires when the mouse is moved over the TextBox
Using If.....Then.....Else Statements with Operators
To effectively control the VB program flow, we shall use If...Then...Else statement together
with the conditional operators and logical operators.
If conditions Then
VB expressions
Else
VB expressions
End If
If...Then selection structure
The If...Then selection structure performs an indicated action only when the condition is
True; otherwise the action is skipped.
Syntax of the If...Then selection
If <condition> Then
statement
End If
e.g.: If average>75 Then
txtGrade.Text = "A"
End If
If...Then...Else selection structure
The If...Then...Else selection structure allows the programmer to specify that a different
action is to be performed when the condition is True than when the condition is False.
Syntax of the If...Then...Else selection
If <condition > Then
statements
Else
statements
End If
e.g.: If average>50 Then
txtGrade.Text = "Pass"
Else
txtGrade.Text = "Fail"
End If
Nested If...Then...Else selection structure
Nested If...Then...Else selection structures test for multiple cases by placing If...Then...Else
selection structures inside If...Then...Else structures.
Syntax of the Nested If...Then...Else selection structure
You can use Nested If either of the methods as shown above
Method 1
If < condition 1 > Then
statements
ElseIf < condition 2 > Then
statements
ElseIf < condition 3 > Then
statements
Else
Statements
End If
Method 2
If < condition 1 > Then
statements
Else
If < condition 2 > Then
statements
Else
If < condition 3 > Then
statements
Else
Statements
End If
End If
EndIf
e.g.: Assume you have to find the grade using nested if and display in a text box
If average > 75 Then
txtGrade.Text = "A"
ElseIf average > 65 Then
txtGrade.Text = "B"
ElseIf average > 55 Then
txtGrade.text = "C"
ElseIf average > 45 Then
txtGrade.Text = "S"
Else
txtGrade.Text = "F"
End If
Select...Case selection structure
Select...Case structure is an alternative to If...Then...ElseIf for selectively executing a single
block of statements from among multiple block of statements. Select...case is more
convenient to use than the If...Else...End If. The following program block illustrate the
working of Select...Case.
Syntax of the Select...Case selection structure
Select Case Index
Case 0
Statements
Case 1
Statements
End Select
e.g.: Assume you have to find the grade using select...case and display in the text box
Dim average as Integer
average = txtAverage.Text
Select Case average
Case 100 To 75
txtGrade.Text ="A"
Case 74 To 65
txtGrade.Text ="B"
Case 64 To 55
txtGrade.Text ="C"
Case 54 To 45
txtGrade.Text ="S"
Case 44 To 0
txtGrade.Text ="F"
Case Else
MsgBox "Invalid average marks"
End Select
The syntax of the Select Case control structure is shown below:
Select Case expression
Case value1
Block of one or more VB statements
Case value2
Block of one or more VB Statements
Case Else
Block of one or more VB Statements
End Select
Dim grade As String
Private Sub Compute_Click( )
grade=txtgrade.Text
Select Case grade
Case "A"
result.Caption="High Distinction"
Case "A-"
result.Caption="Distinction"
Case "B"
result.Caption="Credit"
Case "C"
result.Caption="Pass"
Case Else
result.Caption="Fail"
End Select
End Sub
Dim mark As Single
Private Sub Compute_Click()
'Examination Marks
mark = mrk.Text
Select Case mark
Case Is >= 85
comment.Caption = "Excellence"
Case Is >= 70
comment.Caption = "Good"
Case Is >= 60
comment.Caption = "Above Average"
Case Is >= 50
comment.Caption = "Average"
Case Else
comment.Caption = "Need to work harder"
End Select
End Sub
Working with Check box,
Checkbox is used to deal with toggle options, which have only two possible states – true/
false, or yes/no.
Checkbox can be in either of the two states – checked or unchecked. When checkbox is
checked, a tick mark appears in the rectangle. When it is unchecked, the rectangle will be
empty.
Unlike the OptionButton control, the CheckBox control lets you perform multiple selections,
meaning that the end user of your finished software product will be able to have multiple
choices from a list of items.
Imp Notes - This control has three states : checked, unchecked and grayed. The value
property determines the checkbox state.
Example
Private Sub cmdShow_Click()
If Check1.Value = 1 Then
Print "Checked !"
ElseIf Check1.Value = 0 Then
Print "Unchecked !"
End If
End Sub
If you assign Check1.Value = 1 then the CheckBox is checked and if Check1.Value = 0 then
it is unchecked. If Check1.Value = 2 then the checkbox is grayed.
Example
The previous program can also be written in the following way.
Private Sub cmdShow_Click()
If Check1.Value = VBChecked Then
Print "Checked !"
ElseIf Check1.Value = VBUnchecked Then
Print "Unchecked !"
End If
End Sub
vbChecked and vbUnchecked are VB constants whose values are 1 and 0 respectively.
The grayed state can be set using the 'vbGrayed' VB constant whose value is 2.
Example
Program to make multiple choices.
Private Sub cmdShow_Click()
If chkTea.Value = 1 Then
Print "Tea"
End If
If chkCoffee.Value = 1 Then
Print "Coffee"
End If
If chkPizza.Value = 1 Then
Print "Pizza"
End If
If chkChocolate.Value = 1 Then
Print "Chocolate"
End If
End Sub
Apart from checked and unchecked states, there is another state called grayed state. The
grayed state is used to indicate that the checkbox is unavailable.
The grayed state can be set using the value property from the Properties Window or you may
want to set this property in run-time.
Example
Check1.Value = VBGrayed
Or,
Check1.Value = 2
List BOX
ListBox and ComboBox controls present a set of choices that are displayed vertically in a column. If
the number of items exceed the value that be displayed, scroll bars will automatically appear on the
control. These scroll bars can be scrolled up and down or left to right through the list.
Property/Method Description
1. Properties
By setting this property to True or False user can decide
Enabled
whether user can interact with this control or not
Index Specifies the Control array index
String array. Contains the strings displayed in the drop-down
List
list. Starting array index is 0.
ListCount Integer. Contains the number of drop-down list items
Integer. Contains the index of the selected ComboBox item. If
ListIndex
an item is not selected, ListIndex is -1
Boolean. Specifies whether user can type or not in the
Locked
ComboBox
Integer. Specifies the shape of the mouse pointer when over the
MousePointer
area of the ComboBox
Integer. Index of the last item added to the ComboBox. If the
NewIndex
ComboBox does not contain any items , NewIndex is -1
Boolean. Specifies whether the ComboBox's items are sorted
Sorted
or not.
Style Integer. Specifies the style of the ComboBox's appearance
Boolean. Specifies whether ComboBox receives the focus or
TabStop
not.
Text String. Specifies the selected item in the ComboBox
String. Specifies what text is displayed as the ComboBox's tool
Tool Tip Index
tip
Boolean. Specifies whether ComboBox is visible or not at run
Visible
time
2. Methods
AddItem Add an item to the ComboBox
Clear Removes all items from the ComboBox
RemoveItem Removes the specified item from the ComboBox
SetFocus Transfers focus to the ComboBox
3. Event Procedures
Change Called when text in ComboBox is changed
DropDown Called when the ComboBox drop-down list is displayed
GotFocus Called when ComboBox receives the focus
LostFocus Called when ComboBox loses it focus
Adding items to a List
It is possible to populate the list at design time or run time
Design Time : To add items to a list at design time, click on List property in the property box
and then add the items. Press CTRL+ENTER after adding each item as shown below.
Run Time : The AddItem method is used to add items to a list at run time. The AddItem
method uses the following syntax.
Object.AddItemitem, Index
The item argument is a string that represents the text to add to the list
The index argument is an integer that indicates where in the list to add the new item. Not
giving the index is not a problem, because by default the index is assigned.
Following is an example to add item to a combo box. The code is typed in the Form_Load
event
Private Sub Form_Load()
Combo1.AddItem 1
Combo1.AddItem 2
Combo1.AddItem 3
Combo1.AddItem 4
Combo1.AddItem 5
Combo1.AddItem 6
End Sub
Removing Items from a List
The RemoveItem method is used to remove an item from a list. The syntax for this is given
below.
Object.RemoveItem index
The following code verifies that an item is selected in the list and then removes the selected
item from the list.
Private Sub cmdRemove_Click()
If List1.ListIndex > -1 Then
List1.RemoveItem List1. ListIndex
End If
End Sub
Sorting the List
The Sorted property is set to True to enable a list to appear in alphanumeric order and False
to display the list items in the order which they are added to the list.
Using the ComboBox
A ComboBox combines the features of a TextBox and a ListBox. This enables the user to
select either by typing text into the ComboBox or by selecting an item from the list. There are
three types of ComboBox styles that are represented as shown below.
Dropdown Simple combo
Dropdown list
combo
Dropdown Combo (style 0)
Simple Combo (style 1)
Dropdown List (style 2)
The Simple Combo box displays an edit area with an attached list box always visible
immediately below the edit area. A simple combo box displays the contents of its list all the
time. The user can select an item from the list or type an item in the edit box portion of the
combo box. A scroll bar is displayed beside the list if there are too many items to be
displayed in the list box area.
The Dropdown Combo box first appears as only an edit area with a down arrow button at the
right. The list portion stays hidden until the user clicks the down-arrow button to drop down
the list portion. The user can either select a value from the list or type a value in the edit area.
The Dropdown list combo box turns the combo box into a Dropdown list box. At run time ,
the control looks like the Dropdown combo box. The user could click the down arrow to view
the list. The difference between Dropdown combo & Dropdown list combo is that the edit
area in the Dropdown list combo is disabled. The user can only select an item and cannot type
anything in the edit area. Anyway this area displays the selected item.
Combo Boxes
Combo boxes are so-named because they "combine" the features found in both text
boxes and list boxes. Combo boxes are also commonly referred to as "drop-down
boxes" or "drop-down lists". There are three combo box styles:
0 – Drop Down Combo
1 – Simple Combo
2 – Drop Down List
At design-time, you set the style of the combo box with its Style property.
Of the three combo box styles, the one most similar to a ListBox is "2 - Drop-Down
List". In fact, everything discussed in the last several pages regarding ListBoxes
applies to drop-down style combo boxes EXCEPT the following:
· The combo box displays the selected item in the text box portion of the combo
box. The ListBox portion of the combo box remains hidden until the combo box
receives focus and the user clicks the down arrow on the text box.
· You can change the width of a drop-down combo box, but not its height.
· Like a ListBox, and unlike the other combo box styles, the user can only select a
value that is in the list – they cannot type in a new value. However, if the user
does press a keyboard key, the closest item in the list alphabetically will be
selected (i.e., if the user types the letter "B", and the word "Banana" is in the list,
then "Banana" will be selected).
· Multiple selections cannot be made. Only one item may be selected from the
list.
· Unlike a ListBox, VB does not automatically pre-select the first value from a
combo box. Therefore, the initial value of the ListIndex property is –1. If you do
not pre-select a value in code (say in the Form_Load procedure), then be sure to
check the ListIndex property for a value of –1 when you check to see which item
the user selected.
The "0 – Drop Down Combo" style of combo box operates the same way as the Drop
Down List, except that the user may type a new value in the text box portion of the
combo box. Please note that if the user types a non-list value, this value is NOT
automatically added to the list, and the value of ListIndex would be –1. You should
check the Text property to access the user's selection or entry.
The "1 – Simple Combo" style of combo box operates in a manner similar to the
Drop Down Combo in that the user can either select an item from the list or type a
new entry. The difference is that the list does not drop down – like a ListBox, you
determine the height of the list portion at design-time. This is the only combo-box
style that allows you to adjust the height of the list portion.
Combo Box Demo Program
A sample program to demonstrate the three combo box styles will now be presented.
The form at design-time is shown below:
Frame1 has the Caption 'Style "2 – Dropdown List"' and contains a combo box
named cboFood with its Style property set to 2 – Dropdown List. It also contains
three command buttons, named cmdLoadFoodCombo, cmdSelect, and
cmdDetermine. It also contains a checkbox named chkAuto.
Frame2 has the Caption 'Style "0 – Dropdown Combo"' and contains a combo box
named cboStateAbbrev with its Style property set to 0 – Dropdown Combo. It also
contains two command buttons, named cmdLoadStateAbbrevCombo and
cmdAddNewAbbrev.
Frame3 has the Caption 'Style "1 – Simple Combo"' and contains a combo box
named cboStateName with its Style property set to 1 – Simple Combo. It also
contains two command buttons, named cmdLoadStateNameCombo and
cmdAddNewName.
We will first look at the code dealing with the Dropdown List style of the combo box.
The cmdLoadFoodCombo_Click Event
In this event, we first clear the cboFood combo box, then manually add three food
items along with their calorie count in the corresponding ItemData property. (See the
earlier discussion "Getting Data into a Listbox" for more detail.) We then inform the
user that the combo box has been loaded.
Private Sub cmdLoadFoodCombo_Click()
cboFood.Clear
cboFood.AddItem "Orange"
cboFood.ItemData(cboFood.NewIndex) = 60
cboFood.AddItem "Apple"
cboFood.ItemData(cboFood.NewIndex) = 80
cboFood.AddItem "Banana"
cboFood.ItemData(cboFood.NewIndex) = 105
MsgBox "Food combo box has been loaded.", _
vbInformation, _
"Combo Box Demo"
End Sub
Note: When you run the sample application and click this button, you will not initially
see these items, because we are not pre-selecting any item:
You have to click the down arrow on the combo box to see the items:
If you wanted to pre-select an item in the combo box, you would set the ListIndex
property to the index of the desired entry. For example, the statement
cboFood.ListIndex = 0 would pre-select the first item in the box.
The cmdSelect_Click Event
The purpose of the code behind the cmdSelect button is to show how to select a
specific entry from a combo box in code. Given an item to match against, you loop
through the entries in the combo box until you either find a matching entry or you go
through the entire list without finding the entry. If you find the desired entry, you set
the ListIndex property to that of the matching entry and exit the loop early. In this
example, an InputBox prompts the user to enter a food. We then loop through the
entries of the combo box, comparing the current List entry to the food item entered
in the InputBox. If and when the item is found, the ListIndex is set accordingly.
Private Sub cmdSelect_Click()
Dim strFood As String
Dim intX As Integer
Dim blnFound As Boolean
strFood = InputBox("Please enter the food to select
(apple, orange, or banana).", _
"Combo Box Demo - Select with Code")
If strFood = "" Then Exit Sub
blnFound = False
For intX = 0 To cboFood.ListCount - 1
If UCase$(cboFood.List(intX)) = UCase$(strFood) Then
cboFood.ListIndex = intX
blnFound = True
Exit For
End If
Next
If Not blnFound Then
MsgBox "Item was not found in the list.", _
vbExclamation, _
"Combo Box Demo"
End If
End Sub
Below are screen shots of a sample run:
User types "apple" in the InputBox: The item is found in the combo box and
selected:
Note: This exact same technique can be used to match on an ItemData entry in the
list. For example, the code could be modified to accept a number, then you could
loop through the combo box entries looking for an ItemData entry that matched that
number. The check could be done with a statement similar to If
cboFood.ItemData(intX)) = MyNumericEntry Then ...
The cmdDetermine_Click Event
The purpose of the code behind the cmdDetermine button is to show how you
determine and obtain the value of the item that has been selected in the combo box.
Again, our friend the ListIndex property is used. The value of the text portion of the
selected item is given by ComboBoxName.List(ComboBoxName.ListIndex). If
there is a corresponding ItemData item, that value is given by
ComboBoxName.ItemData(ComboBoxName.ListIndex).
Private Sub cmdDetermine_Click()
If cboFood.ListIndex = -1 Then
MsgBox "There is no item currently selected.", _
vbInformation, _
"Combo Box Demo"
Exit Sub
End If
MsgBox "You have selected " &
cboFood.List(cboFood.ListIndex) & "." & vbNewLine _
& "It has " & cboFood.ItemData(cboFood.ListIndex) & "
calories.", _
vbInformation, _
"Combo Box Demo"
End Sub
The cboFood_Click Event
The purpose of the code behind the cboFood_Click event is to show that the value of
the selected items can be obtained as soon as the user selects it from the drop-down
list. This behavior can be examined by first checking the chkAuto checkbox, then by
selecting an item from the cboFood combo box.
Private Sub cboFood_Click()
If chkAuto.Value = vbChecked Then
Call cmdDetermine_Click
End If
End Sub
Next, we will look at the code dealing with the Dropdown Combo style of the combo
box.
The cmdLoadStateAbbrevCombo_Click Event
This event loads the cboStateAbbrev combo box with U.S. state abbreviations from a
comma-delimited sequential file called STATES.DAT. The records in the file contain
two fields, the state abbreviation and state name; only the abbreviations are loaded
into the combo box. No values are added to the optional ItemData property array of
the combo box. As was the case with loading the food combo box, no item is pre-
selected after loading the state abbreviations, so you will not see the entries until you
click the arrow on the drop-down box.
Private Sub cmdLoadStateAbbrevCombo_Click()
Dim intStateFileNbr As Integer
Dim strBS As String
Dim strStateAbbrev As String
Dim strStateName As String
strBS = IIf(Right$(App.Path, 1) = "\", "", "\")
intStateFileNbr = FreeFile
Open (App.Path & strBS & "STATES.DAT") For Input As
#intStateFileNbr
cboStateAbbrev.Clear
Do Until EOF(intStateFileNbr)
Input #intStateFileNbr, strStateAbbrev, strStateName
cboStateAbbrev.AddItem strStateAbbrev
Loop
Close #intStateFileNbr
MsgBox "State abbreviation combo box has been loaded.", _
vbInformation, _
"Combo Box Demo"
End Sub
The cmdAddNewAbbrev_Click Event
This event shows that with this type of combo box, you can type in a new entry in the
textbox portion of the combo box. However, a new entry is not automatically added
to the list portion of the combo box. If you wish to do this, you must do so with code,
as the code in this event procedure demonstrates. When the "Add New Item to List"
button is clicked, this code runs to first check that the item is not already in the list,
and if not, adds it.
Private Sub cmdAddNewAbbrev_Click()
Dim intX As Integer
If cboStateAbbrev.Text = "" Then Exit Sub
For intX = 0 To cboStateAbbrev.ListCount - 1
If UCase$(cboStateAbbrev.Text) = UCase$
(cboStateAbbrev.List(intX)) Then
MsgBox "Item '" & cboStateAbbrev.Text & "' is
already in list.", _
vbExclamation, _
"Combo Box Demo"
Exit Sub
End If
Next
' if we get here, the item entered in text portion is not
in the list
cboStateAbbrev.AddItem cboStateAbbrev.Text
MsgBox "Item '" & cboStateAbbrev.Text & "' has been added
to the list.", _
vbExclamation, _
"Combo Box Demo"
End Sub
Finally, we will look at the code dealing with the Simple Combo style of the combo
box.
The cmdLoadStateNameCombo_Click Event
This event loads the cboStateName combo box with U.S. state names from the
same comma-delimited sequential file called STATES.DAT. The records in the file
contain two fields, the state abbreviation and state name; only the names are loaded
into the combo box. No values are added to the optional ItemData property array of
the combo box. As was the case with loading the food and state abbreviation combo
boxes, no item is pre-selected after loading the data. However, since the list portion
of this style of combo box is visible, you will see the entries after they have been
loaded.
Private Sub cmdLoadStateNameCombo_Click()
Dim intStateFileNbr As Integer
Dim strBS As String
Dim strStateAbbrev As String
Dim strStateName As String
strBS = IIf(Right$(App.Path, 1) = "\", "", "\")
intStateFileNbr = FreeFile
Open (App.Path & strBS & "STATES.DAT") For Input As
#intStateFileNbr
cboStateName.Clear
Do Until EOF(intStateFileNbr)
Input #intStateFileNbr, strStateAbbrev, strStateName
cboStateName.AddItem strStateName
Loop
Close #intStateFileNbr
MsgBox "State name combo box has been loaded.", _
vbInformation, _
"Combo Box Demo"
End Sub
The cmdAddNewName_Click Event
As was the case with state abbreviation combo box, this event shows that with this
type of combo box, you can type in a new entry in the textbox portion of the combo
box. However, a new entry is not automatically added to the list portion of the combo
box. If you wish to do this, you must do so with code, as the code in this event
procedure demonstrates. When the "Add New Item to List" button is clicked, this
code runs to first check that the item is not already in the list, and if not, adds it.
Private Sub cmdAddNewName_Click()
Dim intX As Integer
If cboStateName.Text = "" Then Exit Sub
For intX = 0 To cboStateName.ListCount - 1
If UCase$(cboStateName.Text) = UCase$
(cboStateName.List(intX)) Then
MsgBox "Item '" & cboStateName.Text & "' is
already in list.", _
vbExclamation, _
"Combo Box Demo"
Exit Sub
End If
Next
' if we get here, the item entered in text portion is not
in the list
cboStateName.AddItem cboStateName.Text
MsgBox "Item '" & cboStateName.Text & "' has been added to
the list.", _
vbExclamation, _
"Combo Box Demo"
End Sub
Download the project files for the combo box demo program here.
*** BONUS MATERIAL ***
ComboBox Procedures
Presented below are several examples of how to extend the functionality of the
combobox.
Extend the Number of Items Displayed
By default, when you click the drop-down arrow of a combo box, VB will display a
maximum of eight items. This example shows how you can set up the combo box to
display as many items as you want when the drop-down arrow is clicked.
Download the project files for this example here.
Extend the Width of the Drop-Down List
By default, the drop-down list of a combo box is the same width as the combo box
itself. This example shows how you can set up the combo box so that the width of
the drop-down list is as wide as the widest item in the list.
Download the project files for this example here.
Automatically Drop Down the List when ComboBox Receives Focus
This example shows how you can automatically display the drop-down list (as
opposed to having the user click the drop-down arrow) when the combo box receives
focus.
Download the project files for this example here.
Creating a "Multi-Select" Checkbox-ComboBox
This example shows how you can simulate a combo box where the items in the
drop-down list have checkboxes, enabling you to make multiple selections. The
multiple selections are shown comma-delimited in the text portion of the combo box.
This example does not actually use a combo box – the text portion is actually a
picture box containing a label and a graphical-style command button for the drop-
down arrow. When the command button is clicked, a checkbox-style listbox is
displayed.
In this sample program, when you click the "Show Selected Items" button, a
message box is displayed, showing both the items that were selected, as well as the
corresponding ItemData items, both "as is" as well as enclosed in quotes.
Download the project files for this example here.
Search-As-You-Type ComboBox
This example shows how you implement "search-as-you-type" functionality with a
combo box. As you type characters in the textbox portion of the combo box, the first
item in the list that starts with those characters will be selected. In the screen shot
below, as soon as the user types "o" in the box, "onion rings" is selected. This
example limits the user to items contained in the list.
Download the project files for this example here.
Search-As-You-Type ComboBox (Enhanced Database Example)
This example extends the "search-as-you-type" functionality demonstrated in the
previous example by using database lookups, and this implementation does NOT
limit the user to the items contained in the list. If the user types in an item that is not
already in the list and clicks the Refresh button, the new item will show up the next
time the list is dropped down (i.e., the new item will be inserted into the database
table).
A couple of options can be used to modify the behavior of the combo box. You can
choose to have the program load items based on the first letter typed (i.e., as shown
in the screen shot above, when you type the letter "B", load only the companies that
start with the letter "B"). This option makes sense when you are selecting values
from a large database table. Alternatively, you can choose to have the program load
all values from the database table at once. That option would be suitable for smaller
tables.
Also, you can choose whether or not you want the drop-down box to be displayed as
soon as focus is moved to the combo box.
Download the project files for this example here.