Setting a control focus in OnLoad event.

T

Tony

Based on data present when I load a form, I need to be able to set focus to
a specific control.
I have added code as the last line of my Load handler setting a control to
have focus. e.g. control3.Focus().
But when the form is displayed, the first control with tab stop has focus
instead of the control I set.

This should be a very basic thing to do but I can't figure out how.

In the WIN32 API, you would return false form the OnInitDialog() method to
allow retention of focus you set programmatically.
Anyone know how to do this?

Thanks,
Tony
 
S

Stoitcho Goutsev \(100\)

Tony,

Control.Focus doesn't work if the control is invisible. During OnLoad no
controls are visible, so Focus() doesn't work.

If you use .NET 2.0 you can do that during Shown event.

For all versions instead calling control3.Focus() in the Load event handler
you can do:
this.ActiveControl = control3;
where *this* is the form.

Forst solution is only for .NET 2.0 the second is for all versions of .NET.
 
T

Tony

The ActiveControl property worked, I don't recall ever seeing that for some
reason.

Thanks,
Tony
 

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