Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a form in Visual basic and a textbox into it. Whenever i start the form[debug it :p] (press F5) the textbox automatically gains focus. I dont want it to gain focus, any way to do that?
Posted
Updated 4-Mar-16 3:59am
v2

Set the tab order so that a different control that can accept the user input focus has a lower number: the OK button, for example.


"It works but only with buttons and textbox,RTF boxes ones ! i dont need it but is there any way to work it with picture box or labels something... Just curious :)
"



:laugh: No! Think about it - it's the keyboard input: so it skips all controls that can't take input, (like labels and so forth). Otherwise you could have
Label     TextBox
Username: ..........

and the focus would go to the label instead of the text box. And you want the label to have a tab order value one less than the matching textbox, so that "&" processing works as well. I.e. if the label text in the above example is :
&Username:
Then it displays as "Username", but hold down ALT and the "U" is underlined, so ALT+U moves focus to the Textbox that follows the label.
 
Share this answer
 
v2
Comments
AbhinavGauniyal 2-Aug-13 8:34am    
Ok let me try it and by the way thanks very very much , this community is superb !!!
AbhinavGauniyal 2-Aug-13 8:38am    
It works but only with buttons and textbox,RTF boxes ones ! i dont need it but is there any way to work it with picture box or labels something... Just curious :)
OriginalGriff 2-Aug-13 9:29am    
Answer updated
AbhinavGauniyal 4-Aug-13 0:17am    
Yups! Thanks for the very-well-explained Answer! Now i am starting to love the coding!
OriginalGriff 4-Aug-13 4:06am    
Good!
You're welcome!
"Focus" always means "keyboard focus". You can have focus on something else, some other control which allows user input. Not having keyboard focus when a form gets activated makes no sense at all.

Besides, it's the best to set focus to some control of your choice only once, when the form is shown for the very first time. At all other times, it should be user's choice and nothing else. The System.Windows.Forms library provides a mechanism for setting a keyboard focus on the control which was focused before. You should never shift this focus: a user does it. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.activecontrol.aspx[^].

—SA
 
Share this answer
 
v2
Comments
AbhinavGauniyal 4-Aug-13 0:16am    
Thanks very much! Now i understand it clearly!
Sergey Alexandrovich Kryukov 4-Aug-13 1:19am    
Great. You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900