Sunteți pe pagina 1din 5

OTHER VISUAL BASIC CONTROLS

CS 213: Computer Programming 2


Microsoft Visual Basic 6.0
7
Working with ListBoxes

ListBoxes can be set up to allow user to select one item from the list, or to allow multiple selections. If your
list is too long for the size you’ve drawn the ListBox, scroll bars will appear automatically. Each item in a
ListBox has a corresponding index, and you can refer to the item in the ListBox using this index. When the
user selects (click) a list item, it is highlighted and the ListBox’s ListIndex property is automatically changed
to hold a value corresponding to the item. The first item in the list gets the index 0, the next index 1, and so
on. Use the ListIndex property by itself to determine what the user selected, or use it in combination with the
List property.

Private Sub lstChoices_Click()


Msgbox = “You selected “ & lstChoices.List(lstChoices.ListIndex)
End Sub

Just remember that the ListIndex property is the number of the selected item and the List property is the
text.

RESPONDING TO A LISTBOX EVENTS

You use two main events with listbox: Click and DblClick. How you actually use them is up to you, because
different programs have different needs. You use Click event just as you use the Click event in a button,
with a Click event handler. In this example, we display the item in the listbox that the user has clicked, using
the ListIndex property.

Private Sub lstChoices_Click()


MsgBox “You selected “ & lstChoices.List(lstChoices.ListIndex)
End Sub

And displaying the selected item is the same for DblClick you just add a DblClick handler with the code you
want:

Private Sub lstChoices_DblClick()


MsgBox “You selected “ & lstChoices.List(lstChoices.ListIndex)
End Sub

TEXT PROPERTY

An easier way to get the selected item is to use the Text property. For example:

lstChoices.Text

This code is equivalent to

lstChoices.List(lstChoices.ListIndex)

When no item is selected, the value of ListIndex will equal to -1. You could use that in an If statement to run
code which required a selected item.

If lstChoices.ListIndex = -1 Then
Msgbox “No item selected.”
Else
Msgbox “You selected “ & lstChoices.Text
End If

Information and Communication Technology Department 29


Palompon Institute of Technology
OTHER VISUAL BASIC CONTROLS
CS 213: Computer Programming 2
Microsoft Visual Basic 6.0
7
ADDING ITEMS TO A LISTBOX

You can add items to a list box at design time using the List property. To do this, select the ListBox control
on the form; on the properties window, choose List property and type the item you want to include in the List
(press CTRL + Enter to add another item in the list).

To add item during execution, you can use the AddItem method. This method has one required argument
and one optional argument. For example:

lstChoices.AddItem “Java”
lstChoices.AddItem txtInput.Text
lstChoices.AddItem “Visual Basic”, 2

The line – lstChoices.AddItem “Java” adds the Text – Java at the end of the list. The next line
which is lstChoices.AddItem txtInput.Text adds to the end of the list what the user has
entered in a TextBox named txtInput (You’d probably use a Command Button and put the AddItem
code in the button’s Click event. The last line, lstChoices.AddItem “Visual Basic”, 2 adds
the text Visual Basic to the list and uses an optional argument to give the item a certain place within
the list at the same time. This value would become the new item’s ListIndex and all items already in the
list with this ListIndex or higher would be pushed down one in the list and automatically given a new
ListIndex, obviously one higher.

REMOVING ITEMS FROM A LISTBOX

You can delete items in a ListBox using the RemoveItem method. The RemoveItem method deletes a list
item; its one argument is the ListIndex of the item you wish to remove. Important to consider when
removing list items is that the ListIndex of the remaining items will change to reflect their new positions in
the list. For example, if you want to delete an item that is being selected by the user at Click event of a
command button named cmdDelete:

Private Sub cmdDelete_Click()


If lstChoices.ListIndex = -1 Then
Msgbox “You have not selected an item to delete.”
Else
lstChoices.RemoveItem(lstChoices.ListIndex)
End If
End Sub

THE CLEAR METHOD

The Clear method deletes the entire list at once and requires no arguments.

Private Sub cmdClear_Click()


lstChoices.Clear
End Sub

SORTING ITEMS IN A LISTBOX

You can alphabetize the items in a listbox by setting its Sorted property to True. You should know,
however, that sorting a listbox can change the indexes of the items in that listbox. After the sorting is
finished, the first item in the newly sorted list has index 0, the next index 1, and so on.

DETERMINING HOW MANY ITEMS ARE IN A LISTBOX

You can use the ListCount property to determine how many items are in a listbox.

Information and Communication Technology Department 30


Palompon Institute of Technology
OTHER VISUAL BASIC CONTROLS
CS 213: Computer Programming 2
Microsoft Visual Basic 6.0
7
USING MULTISELECT LISTBOX

A multiselect listbox allows the user to select a number of items at one time. You make a listbox into a
multiselect listbox with the MultiSelect property. The user can then select multiple items using the Shift and
Ctrl keys. Here are the possible settings for MultiSelect:

0 – None. Multiple Selection isn’t allowed (this is the Default).


1 – Simple Multiple selection. A mouse click or pressing the spacebar selects or deselects an item in the list.
2 – Extended Multiple selection. Pressing the Shift key and clicking the mouse or pressing the Shift key and
one of the arrow keys extends the selection from the previously selected item to the current item. Pressing
the Ctrl key and clicking the mouse selects or deselects an item in the list.

Let’s see an example of a multiselect listbox at work. In this case, we’ll have two listboxes named list1
and list2, as well as a command button named cmdAdd with caption - Add. Set list1’s MultiSelect
property to 2 – Extended. When the user selects a number of items in list1 and clicks the button
Add, we’ll copy the selected items in list1 to list2. As in this figure:

After clicking the button


– Add, the selected
item in the ListBox on
the left has been
copied into the ListBox
on the right.

The source code for this example would be:

Option Explicit

Dim i As Integer

Private Sub cmdAdd_Click()


For i = 0 To list1.ListCount – 1
If list1.Selected(i) = True Then
list2.AddItem list1.List(i)
End If
Next i
End Sub

Information and Communication Technology Department 31


Palompon Institute of Technology
OTHER VISUAL BASIC CONTROLS
CS 213: Computer Programming 2
Microsoft Visual Basic 6.0
7
Working with ComboBoxes

Most of what you already know about ListBoxes will apply to a ComboBox. Items are added using the
AddItem method, removing an item uses the RemoveItem method, and to clear a ComboBox, you have
to use the Clear method. List, ListIndex, and ListCount properties will also work the same way,
however, ComboBox cannot handle multiple selections.

ComboBoxes have Style property which can only be change at design time. That means you must decide
which style to use at design time before finally displaying it on the form during execution. There are three (3)
styles to consider:

1. The Dropdown Combo is probably the most used style. This style can be drawn as wide as it
needs to be but it’s height is limited to the space needed for one line of text (this is governed by the
Font Size). Using this style, the user will either select an item from the drop-down list or type in
their own.
2. The Simple Combo appears like you’ve drawn a TextBox just over a ListBox. If the user highlights
(clicks) one of the items in the ListBox portion, the text automatically appears in the TextBox
portion. This is the only style of ComboBox which will respond to a double click and the double
click must come from the ListBox portion of the control.
3. The Dropdown List style does not take user input. This style works identically to the Dropdown
Combo.

Working with CheckBoxes and Option Buttons

CheckBoxes and Option button have common properties. When interacting with CheckBoxes and Option
button, you have to use the Value property to check if the CheckBox has been checked or not, or if an
Option Button has been selected or not.

The CheckBox has the following option for its Value property:

0 – Unchecked
1 – Checked
2 – Grayed

The Value property for an Option button is a Boolean property – True or False.

Consider the following example, which displays a Message box when the user selects either the Male or
Female checkbox.

Private Sub chkMale_Click()


If chkMale.Value = 1 Then
MsgBox “You have selected Male.”
End If
End Sub

Private Sub chkFemale_Click()


If chkFemale.Value = 1 Then
MsgBox “You have selected Female.”
End if
End Sub

Information and Communication Technology Department 32


Palompon Institute of Technology
OTHER VISUAL BASIC CONTROLS
CS 213: Computer Programming 2
Microsoft Visual Basic 6.0
7
Working with Timer Control

You use a Timer control when you want to execute code at specific intervals. To use a timer, you add a timer
control to your program and set its Interval property. From then on, while the timer is enabled, it creates
Timer events, which are handled in an event handling procedure, like Timer1_Timer(). You place the code
you want executed each interval in that procedure.

INITIALIZING A TIMER CONTROL

In using Timer in Visual Basic, you need to interact with the following Timer properties:

Enabled – determines whether or not the timer creates Timer events.


Interval – sets the number of milliseconds between Timer events.

When you place a timer in your program, you can set its Enabled property to False, which means no Timer
events will occur. When you want to start the timer, you can set Enabled to True.

HANDLING TIMER EVENTS

The main event for timers is the Timer event, and double-clicking a timer at design time creates a handler
function for that event:

Sub Timer1_Timer()

End Sub

All you need to do is to add the code you want executed to this procedure. For example, here we display the
current time in a label named lblDisplay using the Visual Basic Time function:

Sub Timer1_Timer()
lblDisplay.Caption = Time
End Sub

Information and Communication Technology Department 33


Palompon Institute of Technology

S-ar putea să vă placă și