Set focus in form.Activated event

  • Thread starter Thread starter Lars Black
  • Start date Start date
L

Lars Black

I have a form that is created when my application starts. Whenever I want to
show the form, I call ShowDialog from my main form.

On the sub form, I have assigned the Activated event to a FormActivated
method. This is done to set some default values whenever the form is show'd.
The last thing in my FormActivated method is a SetFocus() on a textbox to
set the default focus to this control and also highlight (select) the text
in it.

Th problem is however, that is seems like something else is happening after
the Activated event has finished. I can see that the control is focused and
selected but only for a short while, the the form has no focused field
anymore.

private void InitializeComponent()
{
Activated += new System.EventHandler(FormActivate);
}


private void FormActivate(Object sender, EventArgs e)
{
teStartTime.Text = DateTime.Now.ToShortTimeString();
teStartTime.Focus();
}


What am I missing here?

Cheers,
Lars
 
Hi Lars,

Try latching into the Form.GotFocus event, and applying focus to your
textbox then.

-Katie

This posting is provided "AS IS" with no warranties, and confers no rights.

***.Net Compact Framework Info***
Faq:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.a
spx
QuickStarts: http://samples.gotdotnet.com/quickstart/CompactFramework/
Samples:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/h
tml/CompactfxTechArt.asp

--------------------
| From: "Lars Black" <lcb at sense-it dot dk>
| Subject: Set focus in form.Activated event
| Date: Wed, 17 Dec 2003 11:44:42 +0100
| Lines: 33
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: 217.157.30.66
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
..phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:41125
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| I have a form that is created when my application starts. Whenever I want
to
| show the form, I call ShowDialog from my main form.
|
| On the sub form, I have assigned the Activated event to a FormActivated
| method. This is done to set some default values whenever the form is
show'd.
| The last thing in my FormActivated method is a SetFocus() on a textbox to
| set the default focus to this control and also highlight (select) the text
| in it.
|
| Th problem is however, that is seems like something else is happening
after
| the Activated event has finished. I can see that the control is focused
and
| selected but only for a short while, the the form has no focused field
| anymore.
|
| private void InitializeComponent()
| {
| Activated += new System.EventHandler(FormActivate);
| }
|
|
| private void FormActivate(Object sender, EventArgs e)
| {
| teStartTime.Text = DateTime.Now.ToShortTimeString();
| teStartTime.Focus();
| }
|
|
| What am I missing here?
|
| Cheers,
| Lars
|
|
|
 
If you'll use the Form.GotFocus event you'll have a problem each time a
messagebox apears and steal the form's focus, and after that return the
focus back.

my suggestion is to not to use the showdialog method and use the show method
instead, and right after that do the OnShow stuff.

Tomer.
 
Back
Top