hemo


I am trying to focus a textbox when the form loads. Although TextBox.Focus() works with most events, It do not work when in form load or activated. I would be grateful for help



Re: Textbox Focus From Form Load

Peter Ritchie


The problem with the Load event is it is one event in a series of events currently in the queue. Calling a Focus method while processing the Load event means any subsequent Focus-changing events in the queue will simply overwrite it.

The other problem is the Load event is one event in a series of events that occurs when a Form is created, loaded, and displayed. At the time of the load event not all of these events that will occur are in the queue yet. In the same fashion, those soon-to-be-created events will also overwrite your call to Focus.

You can try using BeginInvoke to put it at the end of the queue, and with some events this will work. I don't particularly recall if Focus is one of those things that won't get overwritten in Load...

In general, focus-changes should occur in an Activated event handler. Alternatively, I believe the same effect can be obtained by setting the forms ActiveControl property in the Load event handler.




Re: Textbox Focus From Form Load

hemo

Many thanks for your prompt response.

I have tried the alternatives you suggest without success. It¡¯s important for user friendliness to have an input textbox focused when the forms loaded. It would be surprising to say the least if MS development engineers have omitted this. In vb6 setfocus worked fine.

Help Please.






Re: Textbox Focus From Form Load

ahmedilyas

just to add, tried doing this in the form Shown event




Re: Textbox Focus From Form Load

Swade

Have you tried setting the tabindex = 0




Re: Textbox Focus From Form Load

Tall Dude

See http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=799840&SiteID=1

You don't have to use 'settings' as in the example.

Just control 'activated' doing the select (focus) one time

as shown in the example.






Re: Textbox Focus From Form Load

nobugz

TabIndex = 0 works, so does Control.Select().





Re: Textbox Focus From Form Load

hemo

Hi All

Sorry for the delay replying, business commitments are a huge inconvenience.

The Shown event as suggested by ahmedilyas seems to work fine, but only for initial form load. If the form is reactivated, at runtime the Focus() statement needs to inserted in the form Activated event.

I am still studying the code in the solution suggested by Tall Dude.

Many thanks to all

Hemo





Re: Textbox Focus From Form Load

weirdbeardmt

Setting the tabindex would presumably ruin the tab order on your form.




Re: Textbox Focus From Form Load

hemo

Thanks for the addition info. I can use the Control, but the Tab will not suit my forms performance, however it¡¯s in my ¡®armoury¡¯ for future applications.

hemo