Results 1 to 8 of 8

Thread: WithEvents

  1. #1
    Join Date
    Jul 2004
    Location
    South Dakota
    Posts
    267

    Unanswered: WithEvents

    To All--

    Is there a way to write a class that will listen into all the events that take place on a form, including the events from the controls on the form?

    What I want to do is listen for all the GotFocus events that take place and then, depending on the control type, invoke some code. I thought that if this was possible it would be better then having to instantiate a new class for every control that I want to listen into on the form's Load.

    Thanks for the help.

    C

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    The only way I can think of is to assign the events on form load, instead of having a fully instantiated class...

    What are you actually trying to achieve?
    George
    Home | Blog

  3. #3
    Join Date
    Jul 2004
    Location
    South Dakota
    Posts
    267
    I was going to write a class that would move a colored box on the form to indicate where the focus was. I only wanted it to highlight textboxes and combo boxes. The thought was that if you could listen into all the events on the form you could then respond to only the ones that you care about.

    Otherwise, you would have to setup the events for controls that you want to listen on. I was trying to make it very generic and reusable. I don't want to setup the events for each control when I design the form. (Lazy I know but work smarter not harder!).

    If you have any other ideas or directions let me know. Thanks.

    C

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Isn't there an event you can already use to do what you want - such as OnChange or similar?
    This would be a form level event rather than a control level one.
    George
    Home | Blog

  5. #5
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Why listen to events?

    Why not just use the ActiveControl to tell you where the focus is.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  6. #6
    Join Date
    Jul 2004
    Location
    South Dakota
    Posts
    267
    ActiveControl will tell me which control I would need to move the box to but how do I know when to move the box. That is why I wanted to listen to events. When a GotFocus event was fired I would check the control and move the box to the appropriate control.

    C

  7. #7
    Join Date
    Jul 2004
    Location
    South Dakota
    Posts
    267
    Well this is what I came up with (with help from a post by Bob Bedell).

    Create a class named clsTextBox with the following code:

    Code:
    Option Compare Database
    Option Explicit
    
    Private m_Box As Rectangle
    Private WithEvents m_TextBox As TextBox
    
    'Setup the new class
    Public Sub Initialize(TBox As TextBox, Box As Rectangle)
        Set m_TextBox = TBox
        m_TextBox.OnGotFocus = "[Event Procedure]"
        m_TextBox.OnLostFocus = "[Event Procedure]"
        
        Set m_Box = Box
    End Sub
    
    'Clean up trash when done
    Private Sub Class_Terminate()
        Set m_TextBox = Nothing
        Set m_Box = Nothing
    End Sub
    
    Private Sub m_TextBox_GotFocus()
        'show box and move around the text box
        With m_Box
            m_Box.Visible = True
            .Left = m_TextBox.Left - 100
            .Top = m_TextBox.Top - 100
            .Width = m_TextBox.Width + 200
            .Height = m_TextBox.Height + 200
        End With
    End Sub
    
    Private Sub m_TextBox_LostFocus()
        'hide the box when the text box looses focus
        m_Box.Visible = False
    End Sub
    Then create a form with a few TextBoxes and a rectangle that will be used to highlight the textbox and insert the following code:

    Code:
    Private colTextBoxes As New Collection  'stores the instances of clsTextBox
    Private ctlTextBox As clsTextBox
    
    Private Sub Form_Load()
        Dim ctl As Control
        Dim i As Integer
    
        'loop through all the text boxes on the form and instantiate 
        'the class and add to collection
        For Each ctl In Me.Controls
            With ctl
                If .ControlType = acTextBox Then
                    Set ctlTextBox = New clsTextBox
                    ctlTextBox.Initialize ctl, Box
                    colTextBoxes.Add ctlTextBox
                End If
            End With
        Next ctl
    End Sub
    Now you have a box that will highlight the textbox on the form that has the focus. And you don't have to write a ton of code for each individual TextBox on the form.

    C

  8. #8
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Seriously, is it worth it? You can't just get users to understand the flashing cursor concept?

    Another way you might consider doing this is just having your text / combo boxes have a transparent background.... then when the focus arrives at one of them, the backcolour will show. If you set your backcolour to red or something, it would be pretty difficult to not know which field has the focus.

    Nonetheless, glad you got something sorted
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

Posting Permissions

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