GotFocus vs MouseDown events

  • Thread starter Thread starter James Wong
  • Start date Start date
J

James Wong

Hi! everybody,

I want my control to have different behavior if it got focus by mouse click
from by keyboard (i.e. Tab or Shift-Tab). I found that GotFocus event
always comes before MouseDown even the I click the control directly. My
problem is that I don't know whether I should do something which is
exclusive for keyboard only under GotFocus event since I don't know how the
control got focus.

On MS KB Article 75411, it seems to be solution of my problem. However, it
uses Windows API and I'm looking for a more "modern" (esp. on .NET
Framework) way to do it. Does anybody know the answer?

Thanks for your attention and kindly advice!

Regards,
James Wong
 
Hi,

Your control will recieve the WM_MOUSEACTIVATE message when it
gets focus from the mouse. If you are creating a new control override
wndproc and look for the message. If you have an existing control you need
to make a class that inherits from the nativewindow class to listen to the
messages.

http://msdn.microsoft.com/library/d...rence/MouseInputMessages/WM_MOUSEACTIVATE.asp

http://msdn.microsoft.com/library/d...fsystemwindowsformsnativewindowclasstopic.asp

Sample class

' NativeWindow class to listen to operating system messages.

Private Class MyListener

Inherits NativeWindow

Public Event ClickActivate(ByVal sender As Object, ByVal e As EventArgs)

Const WM_MOUSEACTIVATE = &H21



Private ctrl As Control

Public Sub New(ByVal ctrl As Control)

AssignHandle(ctrl.Handle)

End Sub

Protected Overrides Sub WndProc(ByRef m As Message)

' Listen for operating system messages



If m.Msg = WM_MOUSEACTIVATE Then

RaiseEvent ClickActivate(ctrl, New EventArgs)

End If

MyBase.WndProc(m)

End Sub

Protected Overrides Sub Finalize()

ReleaseHandle()

MyBase.Finalize()

End Sub

End Class


How to use

Dim WithEvents sl As MyListener

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

sl = New MyListener(TextBox1)

End Sub

Private Sub sl_ClickActivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles sl.ClickActivate

Debug.WriteLine("Click Activate")

End Sub


Ken
 
Back
Top