Selected Prop of List Box Can Cause Click Event & Out of Stack
ID: Q110957
|
The information in this article applies to:
-
Microsoft Visual Basic Standard and Professional Editions for Windows, version 3.0
SUMMARY
The Selected property applies to list box and file list box controls. When
you change the selected state of a list box item by using code to set the
Selected property at run time, a Click event occurs -- just as if you had
actually clicked with the mouse. A Click event does not occur if you set
the item's Selected property to a value that keeps the item's current
selected or unselected state.
You need to take care when changing the Selected property within the Click
event procedure for a list or file list box. Clicking an item in a list
control causes a Click event and can change a selection. That changed
selection can affect whether your code's subsequent change to an item's
Selected property causes another Click event. The resulting second click
event can cause recursion and "Out of Stack Space" or other errors if you
designed your Click event procedure incorrectly. The More Information
section below provides examples showing how to avoid recursion problems.
This behavior is by design.
MORE INFORMATION
The Selected property determines the selection status of an item in a list
box or file list box control. The Selected property is an array of Boolean
values with the same number of items as the List property. The Selected
property is available at run time, but not at design time. Here is the
syntax:
[form.]{filelistbox|listbox}.Selected(index)[ = {True|False}]
The Selected property settings are:
True = The item is selected.
False = (Default) The item is not selected.
The Selected property is particularly useful where users can make multiple
selections. You can quickly check which items in a list are selected. Your
code can use this property to select or deselect items in a list.
If only one item is selected, you can use the ListIndex property to get the
index of the selected item. However, in a multiple selection, the
ListIndex property returns the index of the item contained within the focus
rectangle, whether or not the item is actually selected. Multiple selection
mode can be set with the MultiSelect property.
Changing Item's Selected Property Can Cause Second Click Event
- Start a new project in Visual Basic. Form1 is created by default.
- Add a list box (list1) to Form1. (Or you can use a file list box.)
- Add the following code to the Form Load event:
Sub Form_Load ()
For i = 1 To 5
list1.AddItem Str$(i) 'Add more than 2 items to the list box.
Next
End Sub
- Add the following code to the List1 Click event:
Sub List1_Click ()
Static x ' Preserve the value of x between Click events.
x = x + 1 ' Increment the count of Click events.
Print x ' Print the cumulative number of click events.
' The following statement only causes a second event when an item
' other than the first item is clicked. Clicking the first item
' (item 0) does not cause a second event, because list1.Selected(0)
' is already True:
list1.Selected(0) = True ' Selects the first item.
End Sub
- Start the program, or press the F5 key. Click any list box item other
than the first. That causes two Click events. Click the first item in
the list box. That causes just one Click event. Click more items to
repeat the same behavior. Close the form to end the program.
How to Make File List Box Items That Can Be Scrolled, But Not Selected
- Start a new project in Visual Basic. Form1 is created by default.
- Add a file list box to Form1.
- a. Add the following code to the File1 Click event:
Sub File1_Click ()
' If no item is selected in the file list box, exit the sub:
If File1.ListIndex = -1 Then Exit Sub
File1.Selected(File1.ListIndex) = False
End Sub NOTE: If you left out the above If statement, then the
File1.Selected(File1.ListIndex) statement would give the following
error when you click the file list box:
Invalid property array index (Error 381)
The index value of the File1.Selected() property must be greater
than 0. By default, File1.ListIndex starts as -1.
- Instead, you can add the following code to the File1 Click event. In
this example, the focus always reverts to the first item when you
click any other item. The first item remains unselected (without
highlight). The focus is indicated by a dotted box around the item.
Sub File1_Click ()
' NOTE: If you delete the following line, you get "Out of Stack
' Space" due to recursion (about 30 iterations):
If File1.ListIndex = -1 Then Exit Sub
File1.Selected(0) = True
File1.Selected(0) = False 'Resets first item to nonselected.
End Sub
- Start the program, or press the F5 key. Now you can click items in the
file list box, but they won't be selected or highlighted.
Additional query words:
3.00
Keywords : kbcode PrgCtrlsStd
Version : 3.00
Platform : WINDOWS
Issue type :
|