최고의 답변자
C# - Set Focus on TextBox - Which Event?

질문
-
I have a Form and a Textbox control, once the Form loads up, and I want to set the cursor FOCUS on the TextBox, however, I cannot do this and I have no reason why, please help~~
private void Form_Load(object sender, EventArgs e)
{
this.TextBox1.Focus();
}- 이동됨 OmegaMan 2009년 7월 16일 목요일 오후 4:35 (From:Visual C# Language)
2009년 7월 16일 목요일 오전 8:59
답변
-
The Focus() method can only work if the control window is displayed. It isn't yet when the Load event runs. The workaround is simple:
textBox1.Select();
Setting the TabIndex property value properly is another way. And using Shown.
Hans Passant.- 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
2009년 7월 16일 목요일 오후 9:24 -
You could set the tab order of the TextBox to 0 and ensure that the tab order of the other controls are a number higher than 0. The form should then come up with the TextBox with focus and you should not need any code.
Also, check this out:
Open a form in the designer.
Select View | TabOrder
This feature allows you to easily set the order that the user will access the controls on the form.
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!- 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
2009년 7월 16일 목요일 오후 3:15 -
I finally made it work !!!
Here below is the solution:
-----------------------------------
Put the following "PAIR of code" in the FORM_LOAD event, do NOT put the following code after InitializeComponent();
this.WindowState = FormWindowState.Maximized;
this.TextBox1.Focus();
I don't know why, but this is the only way made it work- 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
2009년 7월 17일 금요일 오전 8:29
모든 응답
-
No, it doesn't work at my side.
It seems like there is not much coding I can "investigate", I just put my code right after InitializeComponent(), not working
then, I put it in Form_Load event, not working either.
Since there is only one single line of code, and there is no "appearing" error I can trace. Very Strange to me....
Pls help ~2009년 7월 16일 목요일 오전 10:14 -
Yes, some Form works, but some are not,
I did set FOCUS right after InitializeComponent();
#1 Login Form <--- SET FOCUS - it works !
#2 MDI parent
#3 MDI child <--- SET FOCUS - it doesn't
About #3, I also put the FOCUS right after InitializeComponent();, how come it doesn't work? Anything I should do in order to "DEBUG" it?
Pls help :-)2009년 7월 16일 목요일 오전 10:37 -
You could set the tab order of the TextBox to 0 and ensure that the tab order of the other controls are a number higher than 0. The form should then come up with the TextBox with focus and you should not need any code.
Also, check this out:
Open a form in the designer.
Select View | TabOrder
This feature allows you to easily set the order that the user will access the controls on the form.
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!- 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
2009년 7월 16일 목요일 오후 3:15 -
To my understanding, can I think the logic as like this
Step 1: InitializeComponent();
Step 2: Textbox1.SetFocus();
I mean, can I force the logic to run .SetFocus() even if the TabOrder has been set differently.
Of course, I can do this without any problem. But I simply got my problem in the following "flow" (i.e. #3)
#1 Login Form <--- SET FOCUS - it works !
#2 MDI parent
#3 MDI child <--- SET FOCUS - it doesn't
A while ago, I found some problem in a function under #2 MDI parent (I used F8 to do Debug), surprisely, I found .SetFocus() was not occured in the order I expect.
Anyway, I just want to say, the following "flow" might be "affected" sometimes, I just found this fact, but don't know how to explain further
Step 1: InitializeComponent();
Step 2: Textbox1.SetFocus();2009년 7월 16일 목요일 오후 3:42 -
The Focus() method can only work if the control window is displayed. It isn't yet when the Load event runs. The workaround is simple:
textBox1.Select();
Setting the TabIndex property value properly is another way. And using Shown.
Hans Passant.- 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
2009년 7월 16일 목요일 오후 9:24 -
I finally made it work !!!
Here below is the solution:
-----------------------------------
Put the following "PAIR of code" in the FORM_LOAD event, do NOT put the following code after InitializeComponent();
this.WindowState = FormWindowState.Maximized;
this.TextBox1.Focus();
I don't know why, but this is the only way made it work- 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
2009년 7월 17일 금요일 오전 8:29 -
Hi iHandler,
We need to know which control gets focused in the Activated event handler by check the ActiveControl property of the form. You can set a break point in the activated handler or use MesssageBox.Show(this.ActiveControl.Name) to know that. Then handle the GotFocus event of the activated control and focus the control which you want to focus. Assume the activated control is textBox2 and you want to focus textBox1. Then you can write code as follow:
private void Form1_Load(object sender, EventArgs e)
{
textBox2.GotFocus += new EventHandler(ctrl_GotFocus);
}
void ctrl_GotFocus(object sender, EventArgs e)
{
textBox1.Focus();
(sender as Control).GotFocus -= new EventHandler(ctrl_GotFocus);
}
Let me know if this helps.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.2009년 7월 17일 금요일 오후 12:55 -
Dear iHandler,
I also got the same problem. The best and simplest solution that I got is:
textBox1.DeselectAll(); //Here, your textbox's name is "textBox1"
So, your program code should be:
private void Form_Load(object sender, EventArgs e)
{
this.TextBox1.Focus();textBox1.DeselectAll();
}
Enjoy friend!
- 답변으로 제안됨 Razib007 2013년 6월 3일 월요일 오전 1:46
2013년 6월 2일 일요일 오전 8:44