Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to focus on special textbox?
Posted
Comments
Sergey Alexandrovich Kryukov 3-May-12 17:17pm    
Reason for my vote of 1
Not clear. None text boxes are "special".
Tag application type or UI library: WPF? Forms? Silverlight? ASP.NET? What?

What's the problem?
--SA
aliprogrammer 3-May-12 17:26pm    
i have 4 textbox in my winform. the first textbox's tab index is 1 and the second one's tab index is 2 and ..... so on form load it focus on first textbox not focusing on the textbox which i want to focused on it for example textbox4.focus()

There are, effectively, three ways that you can set the focus. The first way is to use the ActiveControl property of your form.,
C#
this.ActiveControl = txtSpecial;
The second method is to use the Select method, i.e.
C#
txtSpecial.Select();
The third method is to use the Focus method. This method assumes that the ControlStyles.Selectable bit is set to true for the form, and none of the parent controls of the control you are trying to focus on are disabled.
C#
txtSpecial.Focus();
In all cases, you need to wait for the Loaded event before you can attempt to set the focus.
 
Share this answer
 
Set the focus in the Form Shown event
 
Share this answer
 
This would be how you would set focus on Winform:

VB
public myForm()
{
    // Autogenerated.
    InitializeComponent();
    // Get focus on text box 1.
    textBox1.Focus();
}


This is discussed at http://www.eggheadcafe.com/community/csharp/2/10074071/set-focus-on-text-box-when-loading-winform.aspx[^]
 
Share this answer
 

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