Form Listbox - Issue on Load/Click

tt333

New Member
Joined
Sep 15, 2017
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi Folks

I've got this issue with Loading a form containing a Listbox. The issue is that along with the trigger (ie. on DoubleKlick on the list), items within the listbox are being selected (without intent), Is there a work-around?​
Basically, the mouse click turns on each item that would load on the listbox) (at the same spot)​
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
dbl-clicking an item in the list WILL select the item.
but shouldnt do it at 'form load' unless it has code to do so.
 
Upvote 0
dbl-clicking an item in the list WILL select the item.
but shouldnt do it at 'form load' unless it has code to do so.

thank you for your reply. I reallized that i failed to explain my issue accurately:

So, the trigger to load the form is either a button press on a worksheet, or a doubleclick event on a form. Either way, a (separate) form containing a listbox is loaded.
And, as it happens, the last click Action is unluckily registered on the form (as the forms load in the middle of the Excel Window, the likelihood hitting one of the listbox items is very high
 
Upvote 0
This is a timing issue.

Workarounds:

Codes below assume UserForm2 is the form with the ListBox1 . change to suit.

Solution 1) You can add a small delay before showing the second form that has the listbox.

VBA Code:
Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Wait 0.2
    UserForm2.Show
End Sub

Private Sub Wait(ByVal Secs As Single)
    Dim t As Single
    t = Timer: Do: DoEvents: Loop Until Timer - t >= Secs
End Sub


Solution 2) the above *Wait* workaround may not be consistent accross diff users particularly with modeless forms... so, I would suggest using the following better alternative:

VBA Code:
Option Explicit

Private WithEvents Lbx As MSForms.ListBox

Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    With UserForm2
        Set Lbx = .ListBox1
        .ListBox1.Locked = True
        .Show
    End With
End Sub

Private Sub Lbx_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Lbx.Locked And Button = 0 Then
        Lbx.Locked = False
    End If
End Sub
 
Last edited:
Upvote 0
This is a timing issue.

Workarounds:

Codes below assume UserForm2 is the form with the ListBox1 . change to suit.

Solution 1) You can add a small delay before showing the second form that has the listbox.

VBA Code:
Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Wait 0.2
    UserForm2.Show
End Sub

Private Sub Wait(ByVal Secs As Single)
    Dim t As Single
    t = Timer: Do: DoEvents: Loop Until Timer - t >= Secs
End Sub


Solution 2) the above *Wait* workaround may not be consistent accross diff users particularly with modeless forms... so, I would suggest using the following better alternative:

VBA Code:
Option Explicit

Private WithEvents Lbx As MSForms.ListBox

Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    With UserForm2
        Set Lbx = .ListBox1
        .ListBox1.Locked = True
        .Show
    End With
End Sub

Private Sub Lbx_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Lbx.Locked And Button = 0 Then
        Lbx.Locked = False
    End If
End Sub
That's cool. Needed a bit of experimenting, but i went now with Solution2 (but omitted the 'WithEvents...' Part; why is it needed ?)
 
Upvote 0
That's cool. Needed a bit of experimenting, but i went now with Solution2 (but omitted the 'WithEvents...' Part; why is it needed ?)
I guess you are using a Modeless userform. ... If the second userform (where the Listbox is located) is Modal, you will see that it dosn't work unless you use WitheEvents.
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,301
Members
449,095
Latest member
Chestertim

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top