ListBox DblClick event not firing within class module

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bobkohn
    New Member
    • May 2010
    • 3

    ListBox DblClick event not firing within class module

    I have a form in Access 2007 with many listbox controls on it. I want to run a certain routine that does something with the highlighted value in the listbox whenever any of them are double clicked. But it does not work.

    I created a class that is supposed to handle this. Very simplified, and named "clsMyClass ", it looks like this:
    Code:
    Private WithEvents lstThisBox As Access.ListBox
    
    Public Property Set ThisList(ByRef lst As Access.ListBox)
        Set lstThisBox = lst
    End Property
    
    Private Sub lstThisBox_DblClick(Cancel As Integer)
        Call MyRoutine(lstThisBox)
    End Sub
    To demonstrate the problem, I created a form with a listbox named "List5" to use the class to handle it:
    Code:
    Private clsListBox As clsMyClass
    
    Private Sub Form_Load()
        Set clsListBox = New clsMyClass
        Set clsListBox.ThisList = Me.List5
    End Sub
    This does not work: when I double-click the List5 control, "MyRoutine" does not run. However, it does run if I define the DblClick event in the form -- even without anything in it, i.e.:
    Code:
    Private Sub List5_DblClick(Cancel As Integer)
    End Sub
    Can anyone explain this? Is there any way around this? Thanks for your help on this ...

    --bobkohn
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by bobkohn
    I have a form in Access 2007 with many listbox controls on it. I want to run a certain routine that does something with the highlighted value in the listbox whenever any of them are double clicked. But it does not work.

    I created a class that is supposed to handle this. Very simplified, and named "clsMyClass ", it looks like this:
    Code:
    Private WithEvents lstThisBox As Access.ListBox
    
    Public Property Set ThisList(ByRef lst As Access.ListBox)
        Set lstThisBox = lst
    End Property
    
    Private Sub lstThisBox_DblClick(Cancel As Integer)
        Call MyRoutine(lstThisBox)
    End Sub
    To demonstrate the problem, I created a form with a listbox named "List5" to use the class to handle it:
    Code:
    Private clsListBox As clsMyClass
    
    Private Sub Form_Load()
        Set clsListBox = New clsMyClass
        Set clsListBox.ThisList = Me.List5
    End Sub
    This does not work: when I double-click the List5 control, "MyRoutine" does not run. However, it does run if I define the DblClick event in the form -- even without anything in it, i.e.:
    Code:
    Private Sub List5_DblClick(Cancel As Integer)
    End Sub
    Can anyone explain this? Is there any way around this? Thanks for your help on this ...

    --bobkohn
    Just subscribing, will look into this matter a little later.
    1. Where does MyRoutine() reside and what is its Scope?
    2. Kindly post the Code for MyRoutine().

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Access doesn't bother raising an Event for an Outside Object to Sink unless it thinks that it must. You must make sure that all Events you wish to Trap include the Text "[Event Procedure]" in the associated Property. The code listed below will work. Pay special attention to Code Block #1, Line #7.
      1. 'In the Class Module clsMyClass
        Code:
        Private WithEvents lstThisBox As Access.ListBox
        
        Public Property Set ThisList(ByRef lst As Access.ListBox)
          Set lstThisBox = lst
          
          'Let's Trap the Double Click Event only
          lst.OnDblClick = "[Event Procedure]"
        End Property
        
        Private Sub MyRoutine(lst As Access.ListBox)
          MsgBox "ListCount Property of " & lst.Name & " = " & lst.ListCount & _
                 vbCrLf & vbCrLf & "Item Selected is: " & lst.ItemData(lst.ListIndex)
        End Sub
      2. In Form's Declarations
        Code:
        Private clsListBox As clsMyClass
      3. In the Form's Load() Event
        Code:
        Private Sub Form_Load()
          Set clsListBox = New clsMyClass
          Set clsListBox.ThisList = Me.List1
        End Sub
      4. Any questions, feel free to ask.

      Comment

      • bobkohn
        New Member
        • May 2010
        • 3

        #4
        Thank you, ADezii, for figuring this out so quickly. I know where to ask for answers from now on ...

        --bobkohn

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by bobkohn
          Thank you, ADezii, for figuring this out so quickly. I know where to ask for answers from now on ...

          --bobkohn
          You are quite welcome. Be advised that you can also set this Property for all List Boxes either manually or programmaticall y, as in:
          Code:
          Dim ctl As Control
          
          For Each ctl In Me.Controls
            If ctl.ControlType = acListBox Then
              ctl.OnDblClick = "[Event Procedure]"
            End If
          Next

          Comment

          Working...