Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to set focus on dynamically created controls using C#.net windows application

C#
txtsrno = new TextBox();
            txtsrno.Name = "a1" + nn.ToString();
            txtsrno.Location = new Point(6, y);
            txtsrno.Size = new Size(59, 20);
            txtsrno.Text = sno.ToString();
            txtsrno.TabIndex = txt_amount.TabIndex + 1; // this is not working. 
            panel1.Controls.Add(txtsrno);
Posted
Comments
Nicholas Butler 5-Oct-11 8:58am    
Your question and your code sample do not match - are you trying to set the focus or the TabIndex property?
ChiragBMakwana 5-Oct-11 9:07am    
i have three textbox Srno,Itemname,amount.
when i leave from amount then set the focus on itemname.
but problem is that when i leave the focus on amount. focus is set on dicrectly on srno..


when i leave from amount at that time textbox are create dynamically.
so please tell me how to set focus in itemname textbox..

Thanks in advance.
Sergey Alexandrovich Kryukov 5-Oct-11 12:59pm    
Still no match with your code sample -- there is no "itemname". Not to worry, you got a general answer.
--SA

A short note to the correct answer by Mario, just to close the topic: all controls are always created and added dynamically. If you think the Designer does something different, think again. The designer only helps you to create the code during design-time. What happens during runtime is exactly what you do in your application. Look at the C# code auto-generated by the Designer to see it.

—SA
 
Share this answer
 
Once added the control to the panel1, try calling the Focus method.

txtsrno.Focus();


Should help!

Cheers!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Oct-11 12:57pm    
Correct, my 5.
I have a minor contribution to the answer, please see my solution.
--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