Ok, there has got to be a simple solutions to this but I can't find it...
With newly created forms with .NET, how on earth do I select which control is to have the focus by default???
With VB 6 it used to be a simple case of setting the tab order - the first in order would have the focus when the form loaded.
With .NET however, this is no longer true - and even using a SetFocus command on the Load and Activated events will not shift the focus.
This is most irtritating since I have created a logon box but the password textbox simply will not have focus when it loads up resulting in an awkward user interface.
As long as you make sure that you re-number every control, start at zero, and don't duplicate or skip numbers, it should work fine by changing the tab index. I just tested on mine, it worked.
Yep, tried all that... there are a total of 7 controls on this form, a title label, a label and textbox for the user id and password and ok and cancel buttons.
I have set the tab order starting from zero at the password textbox and stepped up 1 for all other controls... still doesn't work.
Nothing seems to have the focus to start with, then I press tab once and I'm on the password text box?
Stranger still, it works fine on another form - a properties window - the focus starts with the first item in the tab order, even when I change it!
Tab order on the logon form is... (no other controls on the form)
Control - Tab Index
lblHeader - 4
lblUserID - 5
txtUserID - 6
lblPassword - 0
txtPassword - 1 -should thus have focus be default!
btnOK - 2
btnCancel - 3
Starts up and nothing visibly has focus until I press the tab key - at which point txtPassword has the focus.
lblPassword has to be 0 (before txtPassword) in order for the keyboard shortcut to work.
My other properties form works fine, i.e. tab order starts lblUserID as 0 and txtUserID as 1. Load the form and txtUser has the focus straight away! Then if I change the tab order, the default focused control also changes - as you would expect! So....???!!!
Why!!! What??? I must be missing something silly here!?!?
If you want to force focus on a control, just use the Form Load event.
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtPassword.focus()
End Sub
That should do exactly what you want.
Of course, I'm not sure how the tab order would work with that. I think that if you hit "Tab" it would go on to the next sontrol like it should, but I'm not sure.
Take my love
Take my land
Take me where I cannot stand
I don't care, I'm still free
You can't take the sky from me...
Yep, is a normal textbox - although it does use a password char, but then I've tried it without that too.
Form is a Fixed dialog, but have changed that too.
Dunno... have tried re-creating all the controls, no luck.
Will try recreating the entire form I guess...
I had the same problem to. I tried to set the focus to a control on load but it wouldn't do it. I was able to set the focus to the control with form activated. I know this may not answer your question, as I wish someone could tell me why it doesn't work. But if you need a quick fix try what I did. Let me know if it works for you also.