Setting focus in OnLoad not working

D

Daniel Carlsson

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
 
H

Herfried K. Wagner [MVP]

* "Daniel Carlsson said:
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.
 
D

Daniel Carlsson

Herfried K. Wagner said:
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
 
A

AlexS

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
 
Y

Ying-Shen Yu[MSFT]

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.
 
D

Daniel Carlsson

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
 
D

Daniel Carlsson

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
 
Y

Ying-Shen Yu[MSFT]

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.
 

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