473,387 Members | 1,693 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,387 software developers and data experts.

How to detect the changing of ActiveControl property on a winform?

Each form has its ActiveControl property, but no ActiveControlChanged
event to signal that the active control has changed (like
ParentChanged event).
I tried to trick the form by declaring this property in my form:

public new Control ActiveControl {
get{ return base.ActiveControl; }
set{ base.ActiveControl = value; }
}

but the execution won't at the breakpoint defined on "set" method of
the property.
This means that the code is never executed.

Maybe some workarounds exist, like handling the Enter event on ALL
controls of my form, but it's not a solution for me.

Any idea?
Thanks.
Dec 13 '07 #1
3 9405
I would create an implementation of IMessageFilter and register it with
the Application class (through the AddMessageFilter method). In it, I would
look for the WM_SETFOCUS and WM_KILLFOCUS windows messages. They will
basically tell you when the focus shifts from control to control (you will
have to look at the window handles, but that's not too hard).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"nano2k" <ad***********@ikonsoft.rowrote in message
news:e7**********************************@s8g2000p rg.googlegroups.com...
Each form has its ActiveControl property, but no ActiveControlChanged
event to signal that the active control has changed (like
ParentChanged event).
I tried to trick the form by declaring this property in my form:

public new Control ActiveControl {
get{ return base.ActiveControl; }
set{ base.ActiveControl = value; }
}

but the execution won't at the breakpoint defined on "set" method of
the property.
This means that the code is never executed.

Maybe some workarounds exist, like handling the Enter event on ALL
controls of my form, but it's not a solution for me.

Any idea?
Thanks.
Dec 13 '07 #2
On Thu, 13 Dec 2007 15:04:44 -0800, nano2k <ad***********@ikonsoft.ro
wrote:
Each form has its ActiveControl property, but no ActiveControlChanged
event to signal that the active control has changed (like
ParentChanged event).
I tried to trick the form by declaring this property in my form:

public new Control ActiveControl {
get{ return base.ActiveControl; }
set{ base.ActiveControl = value; }
}

but the execution won't at the breakpoint defined on "set" method of
the property.
This means that the code is never executed.
There are two issues here. One is that the property isn't a virtual
property, and so all you are doing is hiding the original version (thus
the "new" instead of "override"). The only way your version would be
called is if it were used from code that knows about your Form-derived
type.

The other is that the property isn't likely to change as a result of
setting. I haven't looked at the implementation, but my guess is that the
property maps directly (or nearly so) to the native Win32 API GetFocus()
and SetFocus() methods. The underlying control implementation isn't going
to go through the property to change the focus; all that happens at a much
lower level.

So even if you could override the property, it's not likely you'd ever see
the setter called for normal focus changes.
Maybe some workarounds exist, like handling the Enter event on ALL
controls of my form, but it's not a solution for me.
Why not? Unfortunately, it's likely to be your only practical option.
The basic issue is that not even the underlying native Win32 API provides
a window-level notification of focus changes, at least not specifically.
I think if you really want a notification-style implementation of this,
you'll have to dive into hooking the window messages (see
http://msdn2.microsoft.com/en-us/library/ms644990.aspx)

For a pure .NET implementation, I suppose you could add a Timer to your
form with some reasonable interval (100-500 milliseconds, for example) and
check the current focus with each tick, maintaining your own current
state. But there are obvious problems with something like that.

Pete
Dec 13 '07 #3
On Thu, 13 Dec 2007 15:45:21 -0800, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
Why not? Unfortunately, it's likely to be your only practical option.
The basic issue is that not even the underlying native Win32 API
provides a window-level notification of focus changes, at least not
specifically. I think if you really want a notification-style
implementation of this, you'll have to dive into hooking the window
messages (see http://msdn2.microsoft.com/en-us/library/ms644990.aspx)
Okay...I didn't know about the Application.AddMessageFilter() method.
Looks like there's a .NET version of window hooks after all, per
Nicholas's reply. :)

Pete
Dec 13 '07 #4

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

Similar topics

5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
1
by: Jeff N | last post by:
It it possible to change a button's or text box's font size during runtime? When I try to build my application with this code: this.ActiveControl.Font.Size = "8.25"; I get this error message:...
1
by: Alex Bibiano | last post by:
I have a winform with a new property. The property type is UserControl. This is my code fro the property: private UserControl userControlDetalle; public UserControl UserControlDetalle { get...
3
by: Charlie | last post by:
In the top portion of the DateTimePicker, where the value of the date is displayed, how can I detect whether the month or day or year is currently focused, or, if ShowCheckBox = True, whether the...
4
by: Aphazel | last post by:
I got serveral textboxes in a form and want to copy/cut/paste text to clipboard from currently selected (focused) textbox. It's simple to cut/copy/paste when using specified control directly ie...
1
by: Gary | last post by:
There are several controls in my form. How to detect currently which control has the forcus and set the forcus to a certain control? Thanks,
8
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I detect Opera/Netscape/IE? ----------------------------------------------------------------------- The...
9
by: Zytan | last post by:
http://msdn2.microsoft.com/en-us/system.windows.forms.control.focus(VS.80).aspx this page says: "Focus is a low-level method intended primarily for custom control authors. Instead, application...
1
by: unknown; | last post by:
hello, i've got a question about active control. i'm buillding an application witch uses me.activeControl a few times to check some things. Now something strange happends.. when i click on...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.