Extended Multi-select listbox_change() event workaround

G

gahagan

Hi,

Here was my issue: the listbox_afterupdate() event was firing when
using the mouse to selected more rows in a listbox, but it was NOT
firing when I used the keyboard - SHIFT+UP or SHIFT+DOWN keys.

Specifically, I had a running-total textbox on the bottom right that
performed some math functions on whatever rows were selected (sum of
cost, price, profit, etc.). It wasn't working correctly with the
keyboard events.

I've seen a lot about this issue, without any solution (that I found,
after a rather brief search). But I think I've figured out a
work-around.

I set the lisbox's gotfocus event to set the form's keypreview property
to TRUE, which causes the form to process the keyup/down events before
the control does.

Sub Listbox1_GotFocus()
Me.KeyPreview=True
End Sub

Then, in the form's KeyUp() event, I put the following code:

If KeyCode = vbKeyUp Or KeyCode = vbKeyDown Then
Me.Listbox1_AfterUpdate
End If

I then set the listbox's lostfocus event to set the form's keypreview
property to FALSE.

Sub Listbox1_LostFocus()
Me.KeyPreview=False
End Sub

This was driving me crazy - hopefully this will help someone else out
there with the same problem. So far, it works like a charm.

HTH,
AG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top