Results 1 to 4 of 4

Thread: How to direct all events from 1 type of control to the same handler? {RESOLVED}

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26

    Unhappy How to direct all events from 1 type of control to the same handler? {RESOLVED}

    How do I direct all the events of comboBox_change to one single handler.
    I need to do this because I have a set of runtime created controls, however, the numbers created is only decided during runtime.

    I am using the add method to create new controls.

    I don't wish to create a lot of withevents mycombo as combobox, then assigning them to each new control.

    Thanks
    Last edited by weiyen; Jan 14th, 2004 at 08:14 PM.

  2. #2
    Addicted Member
    Join Date
    Nov 2003
    Location
    India
    Posts
    227
    Use Windows Hooks.

    try to use the following API's. Get help from MSDN. But this way is very dangerous if you unaware of it. But this is the very powerful API in windows environment I ever seen.

    SetWindowLong.

    Search in MSDN for 'Subclassing', 'Hooking' or 'SetWindowLong'

    http://msdn.microsoft.com/library/de...windowlong.asp
    See you,
    -Jai
    [Friends Never Say Good Bye]

  3. #3
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    The event should have an Index argument, use this to reference each of the runtime created comboxes. If it doesn't set the Index of your design time creted combox to 0, then let the IDE create the event again so it adds the Index arg.

    -adehh

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The easiest method would be to use a control array and not the Controls.Add method. With a control array, ensure one combobox whose Index is 0 is on the form at design time. Then at runtime use the Load method to add x number of controls.

    If you want to stick with Controls.Add and WithEvents, then create your own Combobox class as well as a Collection class. Using these classes you can raise the events for the combobox controls back to the Form.

    I included some comments but mostly assume you can follow this coding logic. If you have any questions on how this works just ask.
    VB Code:
    1. '**** ComboBox Class - called clsComboBox
    2.  
    3. Private WithEvents objComboBox As VB.ComboBox
    4. Private objParent As colComboboxes
    5.  
    6. 'Access to the combobox on a form
    7. Public Property Get ComboBox() As VB.ComboBox
    8.     Set ComboBox = objComboBox
    9. End Property
    10. Public Property Set ComboBox(ComboBoxControl As VB.ComboBox)
    11.     Set objComboBox = ComboBoxControl
    12. End Property
    13.  
    14. 'Although this is creates a circular reference it shouldn't be a
    15. 'problem if handled correctly. Make sure everything is terminated
    16. 'properly
    17. Public Property Set Parent(ComboBoxes As colComboboxes)
    18.     Set objParent = ComboBoxes
    19. End Property
    20.  
    21. 'Tell the collection class to raise the events for this combobox
    22. 'to its form
    23. Private Sub objComboBox_Change()
    24.     objParent.RaiseEvents Me, "Change"
    25. End Sub
    26. Private Sub objComboBox_Click()
    27.     objParent.RaiseEvents Me, "Click"
    28. End Sub
    29.  
    30. Private Sub Class_Terminate()
    31.     Set objCmboBox = Nothing
    32.     Set objParent = Nothing
    33. End Sub
    34.  
    35.  
    36. '**** Collection class - called colComboBoxes
    37. Private mcolComboboxes As Collection
    38.  
    39. 'Events the Form object will see.
    40. 'In this sample we pass the instance of the ComboBox class but
    41. 'they can be easily modified to pass the actual control or the
    42. 'Index of the collection or whatever your needs.
    43. Event ComboboxChange(ComboBoxClass As clsComboBox)
    44. Event ComboBoxClick(ComboBoxClass As clsComboBox)
    45.  
    46. Public Function Add(ComboBoxControl As VB.ComboBox)
    47.     Dim objNew As clsComboBox
    48.     Set objNew = New clsComboBox
    49.    
    50.     Set objNew.ComboBox = ComboBoxControl
    51.     Set objNew.Parent = Me
    52.    
    53.     mcolComboboxes.Add objNew
    54.    
    55.     Set objNew = Nothing
    56.  
    57. End Function
    58.  
    59. 'should only be called by the clsComboBox module and if this were a Dll
    60. 'I would declare them as Friend Sub instead
    61. Public Sub RaiseEvents(ComboBoxClass As clsComboBox, EventName As String)
    62.     Select Case EventName
    63.         Case "Change"
    64.             RaiseEvent ComboboxChange(ComboBoxClass)
    65.         Case "Click"
    66.             RaiseEvent ComboBoxClick(ComboBoxClass)
    67.     End Select
    68. End Sub
    69.  
    70. Public Function Item(Index As Long) As clsComboBox
    71.     Set Item = mcolComboboxes(Index)
    72. End Function
    73.  
    74. Private Sub Class_Initialize()
    75.     Set mcolComboboxes = New Collection
    76. End Sub
    77.  
    78. '**** Code for the Form
    79. Private WithEvents ComboBoxes As colComboboxes
    80.  
    81. Private Sub Form_Load()
    82.     Set ComboBoxes = New colComboboxes
    83.  
    84.     'add two comboboxes to the form using the classes
    85.     ComboBoxes.Add Me.Controls.Add("VB.ComboBox", "Combo1")
    86.     With ComboBoxes.Item(1).ComboBox
    87.         .Move 0, 0, 2000
    88.         .Visible = True
    89.         .AddItem "1"
    90.         .AddItem "2"
    91.         .AddItem "3"
    92.         .AddItem "4"
    93.     End With
    94.    
    95.     ComboBoxes.Add Me.Controls.Add("VB.ComboBox", "Combo2")
    96.     With ComboBoxes.Item(2).ComboBox
    97.         .Move 0, 500, 2000
    98.         .Visible = True
    99.         .AddItem "Red"
    100.         .AddItem "Green"
    101.         .AddItem "Yellow"
    102.         .AddItem "Blue"
    103.     End With
    104. End Sub
    105.  
    106. 'Events raised by the collection class
    107. Private Sub ComboBoxes_ComboBoxClick(ComboBoxClass As clsComboBox)
    108.     Debug.Print ComboBoxClass.ComboBox.Name, ComboBoxClass.ComboBox.Text
    109. End Sub
    110.  
    111. Private Sub ComboBoxes_ComboboxChange(ComboBoxClass As clsComboBox)
    112.     Debug.Print ComboBoxClass.ComboBox.Name, ComboBoxClass.ComboBox.Text
    113. End Sub
    114.  
    115. Private Sub Form_Unload(Cancel As Integer)
    116.     'make sure everything is destroyed.
    117.     'probably should include code to call Controls.Remove
    118.     'for each control added at runtime.
    119.  
    120.     Set ComboBoxes = Nothing
    121. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width