focused control ?

L

Lloyd Dupont

how to find the focused .NET control ?

I'm already able to get HWND of the focused control with the win32 call
GetFocus(), but that doesn't really help ....

basically I want to add copy/paste support...
using the Pasteboard class from OpenNETCF and GetFocus(), GetWindowsText()
win32 call I was able to copy or replace the Text property of any control,
however I would like a better behavior, like testing if the target is a
TextBox and have an appropriate behavior in this case ...

Any tips ?
 
M

Maarten Struys, eMVP

I am not sure if this is the smartest way to do this, perhaps somebody else
has a simpler solution, but here is something that could work in your
situation:

Declare a variable of type object and add an event handler to all your
controls to catch the GotFocus event. Just use one event handler for all
controls (including your form as well). The event handler could look like
this:

private void control_GotFocus(object sender, System.EventArgs e)
{
focussedObject = sender;
}

Now whenever you need to know which control has focus you can use some code
like this (this is the part of my solution I don't like I have to admit):

private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
if ( focussedObject is TextBox && (focussedObject as TextBox) == textBox1 )
{
// take action for a TextBox
}
else if ( focussedObject is Button && (focussedObject as Button) ==
button1 )
{
// take action for a Button
}
else if ( focussedObject is ListBox && (focussedObject as ListBox) ==
listBox1 )
{
// take action for a ListBox
}
else if ( focussedObject is ToolBar && (focussedObject as ToolBar) ==
toolBar1 )
{
// take action for a ToolBar
}
else if ( focussedObject is Label && (focussedObject as Label) == label1 )
{
// take action for a Label
}
else
{
// take action for the Form
}
}

Of course if you don't need to take action on certain controls you can just
leave them out of your check. Hopefully this at least gives you an idea.
 
M

Maarten Struys, eMVP

Of course you can wait and see if somebody else has a better idea. I don't
know if you posted your question on OpenNETCF.org as well. That might not be
a bad idea, perhaps somebody reading the forum there might have another,
more elegant solution.

--
Regards,

Maarten Struys, eMVP
PTS Software bv
----
 
L

Lloyd Dupont

it feel like I'll have to do something like that.
quite daunting, so many screens to change ....
anyway you gave me some impetus (I already had the idea but dismiss it
because it was too tedious ...)
 
M

Maarten Struys, eMVP

Sure, but as far as I understood he has to identify and differentiate
between different controls (take other actions on textboxes than on
listboxes etc.).

--
Regards,

Maarten Struys, eMVP
PTS Software bv
 
A

Alex Yakhnin [MVP]

Why don't you just iterate through a control collection
and find which has the focus? Something like that:

foreach(Control ctl in Forms1.Controls)
{
if (ctl.Focused)
{
//we've got a control with focus
break;
}
}
 
A

Alex Yakhnin [MVP]

That should be easy too:

foreach(Control ctl in Forms1.Controls)
{
if (ctl.Focused)
{
//we've got a control with focus
if (ctl is TextBox)
//do whatever for TextBox

if (ctl is CheckBox)
//do whatever for CheckBox

break;
}
}
 
L

Lloyd Dupont

guys !
I have to bang my head on the wall !
so simple, why didn't I think about it ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top