473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

GotFocus vs MouseDown events

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
Nov 20 '05 #1
2 4149
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/de...SEACTIVATE.asp

http://msdn.microsoft.com/library/de...classtopic.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
------------------
"James Wong" <cp*****@commercialpress.com.hk.NO_SPAM> wrote in message
news:OW**************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #2
Hi! Ken,

It works fine! Thanks for your reply!

Regards,
James

"Ken Tucker [MVP]" <vb***@bellsouth.net> ¦b¶l¥ó
news:eZ**************@tk2msftngp13.phx.gbl ¤¤¼¶¼g...
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/de...SEACTIVATE.asp
http://msdn.microsoft.com/library/de...classtopic.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
------------------
"James Wong" <cp*****@commercialpress.com.hk.NO_SPAM> wrote in message
news:OW**************@TK2MSFTNGP11.phx.gbl...
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


Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Krisa | last post by:
Hello all, I just discovered something (stop me if you've heard this before....) that was causing me a significant performance hit when opening a form with subforms. To speed up loading the...
6
by: lauren quantrell | last post by:
I have a combo box on a form. It uses the GotFocus event to poopulate the rowsource with a value list that is created on the fly in VBA. Fine and dandy, except, is a user opens another form and...
0
by: Tom Hartnett | last post by:
This seems like it ought to be a simple thing. I am long accustomed to using the GotFocus event in VB6 to select the text in a textbox when the control is clicked in or tabbed into. C# doesn't...
1
by: Nathan Sokalski | last post by:
I want to create a pushbutton-like control on my webform that has an image on it. I used to think that this is what the ImageButton control was, but it seems to me that the ImageButton is nothing...
4
by: Colin McGuire | last post by:
Hi again, thanks everyone for your previous help. But having resolved past problems, I'm moving on to new problems :( This one is a simple winforms application with two buttons, named Button1...
4
by: dudzcom | last post by:
hi, this is the first time i've posted to this board, though i have been a devoted lurker for some time i have been working in vb.net and run into a couple problems that seem to require solutions...
2
by: Boki | last post by:
Dear All, What is the different between Enter and GotFocus events? Best reagrds, Boki.
1
by: JDeats | last post by:
It appears the WinForm MouseDown and MouseUp event handlers are not working properly. In the "bare bones" sample application below, Form1_MouseUp gets called even through the mouse button remains...
1
by: moondaddy | last post by:
I have a c# 3.5 wpf app which uses user controls for data entry screens. These data entry screens can be nested inside of each. We can also have several ones open side by side at the same time. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.