If I have an Extended MultiSelect ListBox, how can I update other form fields when the selected values in the listbox are changed? It's not a simple as it looks, because I'm looking for a technique that will work with both a mouse AND the keyboard.
Here's a simple setup to illustrate the problem. Say you have a form with a listbox called [tt]Listbox0[/tt] and a TextBox called [tt]TextBox2[/tt]. The listbox is set to MultiSelect Extended, so that when you press the up- and down-arrow keys, the listbox selection changes. Also, say the listbox uses a Value List row source of "alpha;beta;gamma;delta;epsilon". (It doesn't matter what the row source is, but we'll use this list as an example.)
The code underlying this form is as follows, so that when the listbox's AfterUpdate event fires, the textbox lists the values of the selected items.
When I click listbox items with the mouse, then the AfterUpdate event fires, and the textbox displays the correct values. I click alpha, the textbox shows "alpha". I drag across beta and gamma, the textbox shows "beta, gamma" as expected.
When I use the keyboard, though, ItemsSelected doesn't get updated until after AfterUpdate fires. Say I'm on alpha. Then I press the down-arrow. Now, the listbox has beta selected (remember that the listbox is using Extended MultiSelect), but the textbox still shows "alpha"! If I press down again, then gamma is selected, but "beta" appears in the textbox!
And when I hold down the Shift key and use the arrows, nothing happens at all. It turns out that the key events fire, but not AfterUpdate.
So, what can you suggest as a way around this problem? I'd like to know because in practice, my users would like to use the keyboard to get their reports in a timely manner. One thought I had was to call [tt]ListSelectedItems[/tt] when the listbox's KeyUp event fires, but that can be impractical when the processing is more involved than just listing the selected items.
Here's a simple setup to illustrate the problem. Say you have a form with a listbox called [tt]Listbox0[/tt] and a TextBox called [tt]TextBox2[/tt]. The listbox is set to MultiSelect Extended, so that when you press the up- and down-arrow keys, the listbox selection changes. Also, say the listbox uses a Value List row source of "alpha;beta;gamma;delta;epsilon". (It doesn't matter what the row source is, but we'll use this list as an example.)
The code underlying this form is as follows, so that when the listbox's AfterUpdate event fires, the textbox lists the values of the selected items.
Code:
Private Sub List0_AfterUpdate()
Call ListSelectedItems
End Sub
Private Sub ListSelectedItems()
Dim v As Variant
Dim s As String
For Each v In Me.List0.ItemsSelected
If s <> "" Then
s = s & ", "
End If
s = s & Me.List0.ItemData(CLng(v))
Next v
Me.Text2.Value = s
End Sub
When I click listbox items with the mouse, then the AfterUpdate event fires, and the textbox displays the correct values. I click alpha, the textbox shows "alpha". I drag across beta and gamma, the textbox shows "beta, gamma" as expected.
When I use the keyboard, though, ItemsSelected doesn't get updated until after AfterUpdate fires. Say I'm on alpha. Then I press the down-arrow. Now, the listbox has beta selected (remember that the listbox is using Extended MultiSelect), but the textbox still shows "alpha"! If I press down again, then gamma is selected, but "beta" appears in the textbox!
And when I hold down the Shift key and use the arrows, nothing happens at all. It turns out that the key events fire, but not AfterUpdate.
So, what can you suggest as a way around this problem? I'd like to know because in practice, my users would like to use the keyboard to get their reports in a timely manner. One thought I had was to call [tt]ListSelectedItems[/tt] when the listbox's KeyUp event fires, but that can be impractical when the processing is more involved than just listing the selected items.