Controls Focus

A

ariadna.sirianni

Hi.
I'm developing in C# for PPC devices.

I found some code that works for WindowsForms. The code is the
followin:

DllImport("coredll.dll, EntryPoint="GetFocus")]
private extern static IntPtr GetFocus();
···
control.Focus();
IntPtr handle = GetFocus();

[C#]
public class MyForm : Form
{
[DllImport("user32.dll", CharSet=CharSet.Auto,
CallingConvention=CallingConvention.Winapi)]
internal static extern IntPtr GetFocus();

private Control GetFocusedControl()
{
Control focusedControl = null;
// To get hold of the focused control:
IntPtr focusedHandle = GetFocus();
if(focusedHandle != IntPtr.Zero)
// Note that if the focused Control is not a .Net
control, then this will return null.
focusedControl = Control.FromHandle(focusedHandle);
return focusedControl;
}
}

As you see, GetFocus returns an IntPtr of the focused control, after
that, the function "Control.FromHandle" recives that IntPtr and
returns the focused control.

I need to know if there is some function in .Net Compact Framework
that works as FromHandle (Control.FromHandle) does.

Thanks in advance.

Ariadna.-
 
P

Peter Foot [MVP]

An alternative approach is to loop through the controls in the Controls
collection of your form and check the Focused property e.g.

foreach(Control c in this.Controls)
{
if(c.Focused)
{
//do something with c
}
}

Peter

--
Peter Foot
Device Application Development MVP
www.peterfoot.net | www.inthehand.com

Hi.
I'm developing in C# for PPC devices.

I found some code that works for WindowsForms. The code is the
followin:

DllImport("coredll.dll, EntryPoint="GetFocus")]
private extern static IntPtr GetFocus();
···
control.Focus();
IntPtr handle = GetFocus();

[C#]
public class MyForm : Form
{
[DllImport("user32.dll", CharSet=CharSet.Auto,
CallingConvention=CallingConvention.Winapi)]
internal static extern IntPtr GetFocus();

private Control GetFocusedControl()
{
Control focusedControl = null;
// To get hold of the focused control:
IntPtr focusedHandle = GetFocus();
if(focusedHandle != IntPtr.Zero)
// Note that if the focused Control is not a .Net
control, then this will return null.
focusedControl = Control.FromHandle(focusedHandle);
return focusedControl;
}
}

As you see, GetFocus returns an IntPtr of the focused control, after
that, the function "Control.FromHandle" recives that IntPtr and
returns the focused control.

I need to know if there is some function in .Net Compact Framework
that works as FromHandle (Control.FromHandle) does.

Thanks in advance.

Ariadna.-
 

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