Textbox focus and Showdialog

G

Guest

When I call showdialog on a form, I'd like the focus to be on a certain text
box.
If I say "Form.textBox1.Focus()", then call Form.Showdialog(), the Focus()
call is ignored. I can't seem to find an Form event to trap (like Activate)
that is supported by the .Net Compact Framework. Does anybody know a
supported event that I could trap in this case to set the Focus to a control
on a form after the Showdialog executes?

I've got lots of info on Forms but very little of it pertains to the compact
framework.

thanks

Bill
 
P

Peter Hartlén

Don't name you form instance Form.
Load and Activate is supported in CF, one of them should work for you. Use
something like this:

Form frm = new Form();
frm.Activated += new EventHandler(frm_Activated);

private void frm_Activated(object sender, EventArgs e)

{

txtMyTextbox.Focus();

}

/ Peter
 
G

Guest

Peter

Thanks for responding. I've done a little more experimentation with this,
but without success... I'm using a PocketPC 2002 Emulator...and yes, it does
support the Activated event (I had old documentation) and also GotFocus,
which is fired off afterward, and I have tried your suggestion to no avail.
Yes I can set the focus to a text box as a result of a click, so that works,
but when I try to set the focus on a child control in a handler for
'Activate' or 'GotFocus', apparently the focus is still on the entire form-
like my gotFocus code executes *before* the focus goes to the form. Does
this sound like it might be a problem with the emulator?

Based on my experiments and reading, your solution should be the simple one
that works, except that in my emulator, it doesn't work. If I do exactly
what you suggested, and I have, I still see the form but no focus on the
member text box.

Any other ideas?

Bill
 
J

John Socha-Leialoha

I find that I have to trap the form's GotFocus event in addition to the
Activate event. When you first create an display a form, the form's
GotFocus is triggered later in the cycle, and this is where you want to
set the focus to your text box. The Activate event is useful if your
form is already running, but your application isn't the front-most
application. Your form will get the Activate event when it becomes the
front-most window again.

-- John
 
M

Miquel A. Carrera

You can try to overload the ShowDialog method.

public DialogResult ShowDialog(bool setFocus)
{
if (setFocus)
txtControl.Focus();
return base.ShowDialog();
}
 

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