Question problem in set focus on a dynamic textbox

pooya1072

Well-known member
Joined
Jul 5, 2012
Messages
84
Programming Experience
Beginner
hi friends
i have 2 form on my project.in the secend form has this controls :
1- 5 textbox that add to form at runtime (TextBox(1) , TextBox(2) , .... )
2- 2 button that add at design time (Button1 and Button2)

i set button1 as AcceptButton property for the form.
now i want each time the form is load the first TextBox (Textbox(1) ) get focus . i use this code on FormLoad event :
VB.NET:
Textbox(1).setFocus

first time that i open this form Textbox(1) is focused . but if i click Button1( that set for AcceptbButton for this form) and close the form , next time that i open form Textbox(1) is not selected. only Button1 is selected. how can i set focus for Textbox(1) for each time i open the form?
 
My first question is, if you know that you need five TextBoxes, why are you adding them at run time in the first place? If you know what you need at design time then, more often than not, the logical thing to do is to add the controls at design time.
 
thanks... you're right . but suppose the number of textbox is not specify. in this case please answer my question.
 
Just like any control, you need to get a reference to the TextBox and call its Select method. You can get a reference from the Controls collection of the form or whatever other control you added it to or you can keep your own list of the TextBoxes you create.

That said, if you close the form then the TextBox you created no longer exists. If you create a new instance of that same class then it's created from scratch with new TextBoxes. If you want to remember some settings for that form between instances then you have to save that information somewhere outside the form.
 
Back
Top