Results 1 to 5 of 5

Thread: Trapping all events

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Location
    Kathmandu
    Posts
    3
    Let me put the problem with one simple example.

    I have a form 'Form1' with two controls, one text box control 'Text1' and a command button 'Command1'. I have a method :
    Sub TrapEvent()
    ' need to find which event is to occur, and for what
    ' object: Text1, Command1 or Form1
    End sub

    As we know each object has its own set of events, here the case with Text1 and Command1. Lets take simple Click events :
    Sub Text1_Click()
    ' some statements
    End Sub
    Sub Command1_Click()
    ' some statements
    End Sub

    Now When Command1 is clicked, Command1_Click() event will be triggered and when Text1 is clicked Text1_Click event will be triggered. What I need now is on whatever object(Text1, Command1, Form1) I clicked, my TrapEvent() method should be executed before executing the _Click event of the corresponding controls and thereby I should be able to determine which event ('Click' in this case) is occured and for what control object.

    This is the problem. So all the VB experts are requested to give its solution.

    Thank you all.

  2. #2

    Cool

    Hey GM,

    My best guess is to change Trap Event a little:

    Code:
    Public Sub TrapEvent(ByRef ctlCurrent AS Control, psEvent as String)
    
       If TypeOf ctlCurrent is TextBox then
          Select Case psEvent
             Case "Click"
                MsgBox ctlCurrent.Name & " Click event fired"
          End Select
       Endif
       
    End Sub
    And then put the following code in your event:

    Code:
       Sub Text1_Click() 
          Call TrapEvent(Text1, "Click")   
    
          ' some statements 
       End Sub
    The TrapError procedure could also be changed to a function that returns a boolean and then you can change your code to:

    Code:
       Sub Text1_Click() 
          If TrapEvent(Text1, "Click") = True then
             ' some statements 
          Else
             'som statements
          Endif
       End Sub
    Also, the ByRef in the TrapError procedure allows you to make changes to that control if there is a problem in the information stored in it.

    Hope this helps.

    [Edited by SkippySolutions on 05-31-2000 at 10:43 AM]

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2000
    Location
    Kathmandu
    Posts
    3

    Still not getting the solution

    Thanks to Skippy Solutions for the suggestion. But the solution is not as I expected. I think my question was not clear. My intention is to trap all events that will be occured in any controls in the form by my own procedure i.e. TrapEvent(). In such a case it won't be wise to call the procedure explicitely in every event of every control on the form. I need such a solution with which the system automatically takes the control to the TrapEvent() procedure when any event occured. In this procedure I sould be able to know which event is occured for what control and accordingly I should be able to cancel the event or allow to proceed as usual to corresponding event procedure of the control.

    I hope the question is clear now. I need the solution urgently. So the experts are again requested for this.


  4. #4

    Lightbulb

    Other than the last solution, the only other thing I can think of is to create a hook into the Window's message queue and monitor that. It may take a bit of coding, perhaps a little more than you want to do, but this is the only other solution that I can think of.

  5. #5
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Talking

    HEY. I have a lead for you. This code should set you in the right direction. Didn't know of it's potential before now. I'm looking for the same thing your'e looking for.
    It traps all events in the program you're running. When the program ends, the eventhandling ends too ( i think. or else i gotta find a way to shut it down. To get a number of the type of event, just read the aMsg.message. Ie a mousemove has value 512)

    Code:
    Option Explicit
    
    Private Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
    Private Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long
    
    Const WM_KEYDOWN = &H100
    Const WM_KEYFIRST = &H100
    Const WM_KEYLAST = &H108
    Const WM_KEYUP = &H101
    Const WM_LBUTTONDBLCLK = &H203
    Const WM_LBUTTONDOWN = &H201
    Const WM_LBUTTONUP = &H202
    Const WM_MBUTTONDBLCLK = &H209
    Const WM_MBUTTONDOWN = &H207
    Const WM_MBUTTONUP = &H208
    Const WM_MOUSEMOVE = &H200
    
    Private Type POINTAPI
      x As Long
      y As Long
    End Type
    
    Private Type Msg
      hwnd As Long
      message As Long
      wParam As Long
      lParam As Long
      time As Long
      pt As POINTAPI
    End Type
    
    Sub GetMessage2()
      Dim aMsg As Msg
      Do While GetMessage(aMsg, 0, 0, 0)
        If aMsg.message = WM_KEYDOWN Then
          If aMsg.wParam = 27 Then MsgBox "ESCAPE pressed"
          MsgBox "KeyDown"
        End If
        If aMsg.message = 1026 Then Exit Do    'if X in the corner is pressed
        Debug.Print aMsg.message
        DispatchMessage aMsg
      Loop 'Loops until program is closed
    End Sub


    [Edited by Thomas Halsvik on 06-07-2000 at 08:36 AM]

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