Hello

I have a few forms where I want to set the focus to a specific control thats
not the first one in the taborder.
Ive tried to do that in OnLoad but that doesnt work, the focus reverts to
the first control.

In one form where I just want the focus to change from a tabcontrols tabs to
the first real control I managed to get it to work by setting the focus in
the VisibleChanged event.
But in another one that I reuse for questions to the user (so the control
will be shown and hidden many times) that doesnt work either.

In what event should I be doing this? I cant change the taborder, it just
wont work with the controls (you cant set a tabcontrol to be selected after
its pages).

Thanks in advance,
Dan

Re: Setting focus in OnLoad not working by hirf-spam-me-here

hirf-spam-me-here
Wed May 12 10:16:45 CDT 2004

* "Daniel Carlsson" <dan@kaplans.se> scripsit:
> I have a few forms where I want to set the focus to a specific control thats
> not the first one in the taborder.
> Ive tried to do that in OnLoad but that doesnt work, the focus reverts to
> the first control.

The focus can only be set if the form is visible, and it's not visible
in the 'Load' event.

> In one form where I just want the focus to change from a tabcontrols tabs to
> the first real control I managed to get it to work by setting the focus in
> the VisibleChanged event.
> But in another one that I reuse for questions to the user (so the control
> will be shown and hidden many times) that doesnt work either.

You can try to temporarily change the tab order.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Re: Setting focus in OnLoad not working by Daniel

Daniel
Wed May 12 10:36:01 CDT 2004

"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:2getghF1kcc4U1@uni-berlin.de...
> The focus can only be set if the form is visible, and it's not visible
> in the 'Load' event.

Oh right, but what event should I use then?

> You can try to temporarily change the tab order.

Id be happy to but unless you have an example of how to make a control
inside a TabPage inside a TabControl to be focused before the TabControl
thats going to get very tricky.

Basically what I need is the first event that occurs after the form has been
displayed and picked its initial control so that I can change it without MS
code changing the focus back again. VisibleChanged doesnt seem to be working
so well, at least not the second time around when the form is just displayed
again.

/Dan



Re: Setting focus in OnLoad not working by AlexS

AlexS
Wed May 12 13:40:59 CDT 2004

Hi, Daniel

in worst case you can override WndProc of the form and intercept
wm_showwindow message. Another way, which could be useful is to hook mouse
hover event and add there some logic, which will set focus properly in
parented control.

HTH
Alex

"Daniel Carlsson" <dan@kaplans.se> wrote in message
news:%23UAwVbDOEHA.2728@TK2MSFTNGP12.phx.gbl...
> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
> news:2getghF1kcc4U1@uni-berlin.de...
> > The focus can only be set if the form is visible, and it's not visible
> > in the 'Load' event.
>
> Oh right, but what event should I use then?
>
> > You can try to temporarily change the tab order.
>
> Id be happy to but unless you have an example of how to make a control
> inside a TabPage inside a TabControl to be focused before the TabControl
> thats going to get very tricky.
>
> Basically what I need is the first event that occurs after the form has
been
> displayed and picked its initial control so that I can change it without
MS
> code changing the focus back again. VisibleChanged doesnt seem to be
working
> so well, at least not the second time around when the form is just
displayed
> again.
>
> /Dan
>
>



RE: Setting focus in OnLoad not working by v-yiy

v-yiy
Thu May 13 01:39:16 CDT 2004

Hi Dan,

You may try using the Control.BeginInvoke method to workaround this issue.
<code>
protected override void OnLoad(EventArgs e)
{
BeginInvoke(new MethodInvoker(test));
base.OnLoad (e);
}
void test()
{
System.Diagnostics.Debug.Assert(textBox2.CanFocus);
textBox2.Focus();
}
</code>
Does it resolve your problem?
Feel free to reply this thread if the problem persists.

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.


Re: Setting focus in OnLoad not working by Daniel

Daniel
Thu May 13 03:57:15 CDT 2004

Hi Ying-Shen

That does work, but as its being exectued asynchronously its not guaranteed
to work every time?

According to the documenation:
"Executes the specified delegate asynchronously on the thread that the
control's underlying handle was created on."
But further down it says:
"The BeginInvoke method calls the specified delegate back on a different
thread pool thread."

So which is it? Will BeginInvoke always be called on the same thread as the
control (thus, hopefully, always being executed in the same order) or on
another thread?

And if it does work every time, wont I need to call EndInvoke at some point
when using Control.BeginInvoke? Or does the control handle that itself? (The
documentation doesnt warn that you have to call endinvoke but it doesnt say
you dont have to either.)

/Dan

""Ying-Shen Yu[MSFT]"" <v-yiy@online.microsoft.com> wrote in message
news:Si$QKULOEHA.2224@cpmsftngxa10.phx.gbl...
> Hi Dan,
>
> You may try using the Control.BeginInvoke method to workaround this issue.
> <code>
> protected override void OnLoad(EventArgs e)
> {
> BeginInvoke(new MethodInvoker(test));
> base.OnLoad (e);
> }
> void test()
> {
> System.Diagnostics.Debug.Assert(textBox2.CanFocus);
> textBox2.Focus();
> }
> </code>
> Does it resolve your problem?
> Feel free to reply this thread if the problem persists.
>
> Best regards,
>
> Ying-Shen Yu [MSFT]
> Microsoft Community Support
> Get Secure! - www.microsoft.com/security
>
> This posting is provided "AS IS" with no warranties and confers no rights.
> This mail should not be replied directly, please remove the word "online"
> before sending mail.
>



Re: Setting focus in OnLoad not working by Daniel

Daniel
Thu May 13 04:02:48 CDT 2004

Hi Alex

I caught the VisibleChanged event and that wasnt working when the window was
showed the second time, and if Im right thats the same as overriding WndProc
and listening to wm_showwindow?
Mousehover would sorta work, but the general idea was that the user could
just start to type in information right away when the form shows, the mouse
will probably be in a corner of the screen somewhere (the users dont like
having to use the mouse).

Thanks for the ideas though
/Dan

"AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
news:eqr5deDOEHA.3012@tk2msftngp13.phx.gbl...
> Hi, Daniel
>
> in worst case you can override WndProc of the form and intercept
> wm_showwindow message. Another way, which could be useful is to hook mouse
> hover event and add there some logic, which will set focus properly in
> parented control.
>
> HTH
> Alex



Re: Setting focus in OnLoad not working by v-yiy

v-yiy
Thu May 13 22:03:50 CDT 2004

Hi Daniel,

The Control.BeginInvoke will post a user defined message to the owner
thread of the target control and let that thread execute the delegate
handler. In you case, I assume the owner thread of your control is also the
main thread, so the delegate will be executed on the same thread.

Control.BeginInvoke is of the same name with Delegate.BeginInvoke method
but with different implementations, there is no thread pool involves in
this Control.BeginInvoke, while Delegate.BeginInvoke maintains a thread
pool in internal.

In most cases, we call a delegate without caring about the return value, so
we needn't call the EndInvoke, if you need get the return value of an async
delegate call some time later, you may use EndInvoke with the IAsyncResult
value returned in BeginInvoke, If the asynchronous operation has not been
completed, this function will block until the result is available.

If you still have questions on this issue, please feel free to post in this
thread.

Have a nice day!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.