Results 1 to 1 of 1

Thread: Creating an Event Handler Class

  1. #1

    Thread Starter
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Creating an Event Handler Class

    We've all seen cases where, in a userform, we need multiple controls of the same type to call essentially the same procedure, with some small changes. This is easily achieved by writing a parameter driven procedure and calling it for the appropriate event for each instance of the control type in question.

    i.e. something like this
    VB Code:
    1. Private Sub OptionButton1_Click()
    2.     MyOptionClicker "Option1"
    3. End Sub
    But what do we do when we need to have multiple event procedures for each of a particular type of control and/or we have a large number of controls of that type on the form?

    The answer is to use a class module as an Event Handler for the type of control in question.



    By using a class module as an Event Handler, we will only need to write each event procedure once, within the class, and we will not need to add any event handlers, for that control type, to the forms code page.

    This approach basically involves 3 steps.
    1. Create a Class Module that contains a reference, with events, to the control type in question.
    2. Add the required event handlers to the class module.
    3. Within the _initialize event of the form, create an instance of the event handler for each instance of the control type, and associate that instance of the event handler with that instance of the control type.

    Once these 3 steps are completed, each instance of the control type in question will have a 'hook' into the event handler class and will call the class event handlers whenever needed.


    A Simple Example:
    • The brief is to create a form with 6 OptionButtons.
    • When the mouse is moved over an OptionButton, it should turn Bold and Blue (unless it selected - see next), and revert to "unbold" and black when the mouse moves off it.
    • When an OptionButton is selected, it should turn Bold and Red, and should remain as such until another OptionButton is selected.

    (OK, its a really trivial example, but should get the point across.)

    We could add a separate _MouseMove and _Change event for each of the 6 OptionButtons, but we're going to use a class with a single _MouseMove and _Change event that will work for all the option buttons.

    Step1/ Create a Class Module that contains a reference, with events, to the control type in question.
    We add a class module, which I've called "cOptBtnEventHandler" and add a reference, with events, to the OptionButton object. We also add a read/write property to the class to access this reference.
    VB Code:
    1. ' **************************************************************
    2. ' Class-level Declarations Follow
    3. ' **************************************************************
    4.  
    5. Private WithEvents oOption As msforms.OptionButton
    6.  
    7. ' **************************************************************
    8. ' Class Properties Follow
    9. ' **************************************************************
    10.  
    11. Property Get OptionButton() As msforms.OptionButton
    12.     Set OptionButton = oOption
    13. End Property
    14.  
    15. Property Set OptionButton(OptButton As msforms.OptionButton)
    16.     Set oOption = OptButton
    17. End Property
    Step 2/ Add the required event handlers to the class module.
    We need to handle 2 events for our OptionButtons.
    The _Mousemove event, to trap when the mouse is over the OptionButton, turning the OptionButton Bold and Blue.
    The _Change event, to turn the OptionButton Bold and Red when it is selected, and back to "unbold" and black when it is deselected.

    We add both these event handlers to the "cOptBtnEventHandler" class module.
    VB Code:
    1. ' **************************************************************
    2. ' Class Event Procedures Follow
    3. ' **************************************************************
    4.  
    5.  
    6. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    7. ' Comments: When the option button is selected, Bold the font
    8. '           and change the text color to Red.
    9. '           When deselected, "unbold" the font and change the
    10. '           text colour back to Black.
    11. '
    12. ' Date          Developer       Action
    13. ' --------------------------------------------------------------
    14. ' 21 Jun 06     Declan Kenny    Initial Version
    15. '
    16. Private Sub oOption_Change()
    17.    
    18.     With oOption
    19.        
    20.         If .Value Then
    21.             oOption.Font.Bold = True
    22.             oOption.ForeColor = &HFF& 'Red
    23.         Else
    24.             oOption.Font.Bold = False
    25.             oOption.ForeColor = &H0& 'Black
    26.         End If
    27.     End With
    28.  
    29. End Sub
    30.  
    31. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    32. ' Comments: When the mouse moves over a non-selected
    33. '           OptionButton, change the font to bold and the text
    34. '           color to Blue.
    35. '
    36. ' Date          Developer       Action
    37. ' --------------------------------------------------------------
    38. ' 21 Jun 06     Declan Kenny    Initial Version
    39. '
    40. Private Sub oOption_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    41.     With oOption
    42.         If .Value = False Then
    43.             oOption.Font.Bold = True
    44.             oOption.ForeColor = &HFF0000 'Blue
    45.         End If
    46.     End With
    47. End Sub
    Step 3/ Within the _initilize event of the form, create an instance of the event handler for each instance of the control type, and associate that instance of the event handler with that instance of the control type.
    Before we write the _initialize event, we need to declare a modular level array of our Event Handler class.
    VB Code:
    1. 'Declare an array of our event handler class
    2. Private acMyOpt() As cOptBtnEventHandler

    Now in the _initialize event, we associate an instance of the event handler with each OptionButton.
    VB Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2. ' Comments: When the form is initialized, add an event handler
    3. '           for each OptionButton
    4. '
    5. ' Date          Developer       Action
    6. ' --------------------------------------------------------------
    7. ' 21 Jun 06     Declan Kenny    Initial Version
    8. '
    9. Private Sub UserForm_Initialize()
    10. Dim oMyControl As Control
    11.  
    12.     'Set the initial Scope of the array
    13.     ReDim acMyOpt(0)
    14.    
    15.     For Each oMyControl In Me.Controls
    16.        
    17.         'For all OptionButtons..
    18.         If TypeName(oMyControl) = "OptionButton" Then
    19.            
    20.             '..Add a new instance of the Event Handler Class
    21.             Set acMyOpt(UBound(acMyOpt)) = New cOptBtnEventHandler
    22.            
    23.             '..And point that instance of the class to the current
    24.             'OptionButton
    25.             Set acMyOpt(UBound(acMyOpt)).OptionButton = oMyControl
    26.            
    27.             'Increase the array by one element
    28.             ReDim Preserve acMyOpt(UBound(acMyOpt) + 1)
    29.            
    30.         End If
    31.     Next oMyControl
    32.    
    33.     'select the first OptionButton
    34.     'by default
    35.     With Me.OptionButton1
    36.         .Value = True
    37.         .SetFocus
    38.     End With
    39. End Sub

    And that's pretty much it. Now whenever the _MouseMove or _Change event of an OptionButton is called, the Event Handler's corresponding event will be called. I.e. no need to write separate _event procedures for each OptionButton.

    We do need to add one more procedure to the form. When the mouse is moved off an OptionButton, we need to reset its font. we achieve this by using the Forms _MouseMove event to reset all OptionButtons, except the selected one.
    VB Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2. ' Comments: When the mouse moves off a control, reset all
    3. '           OptionButtons to "unbold" and black, except for the
    4. '           selected OptionButton
    5. '
    6. ' Date          Developer       Action
    7. ' --------------------------------------------------------------
    8. ' 21 Jun 06     Declan Kenny    Initial Version
    9. '
    10. Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    11. Dim oControl As Control
    12.    
    13.     For Each oControl In Me.Controls
    14.        
    15.         If TypeName(oControl) = "OptionButton" Then
    16.            
    17.             If oControl.Value = False Then
    18.                
    19.                 oControl.Font.Bold = False
    20.                 oControl.ForeColor = &H0&
    21.                
    22.             End If
    23.         End If
    24.     Next oControl
    25.  
    26. End Sub

    The attached spreadsheet has the complete code in this example and will show you the event handler in action.
    Attached Files Attached Files
    Last edited by DKenny; Aug 30th, 2006 at 12:49 PM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

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