CF - 将焦点设置到特定控件
我有一个表单,它有一个面板,其中有一些文本框和位于面板外部的复选框。 每次加载表单时,复选框都会获得焦点。
我在表单加载时放置了事件处理程序,并尝试将焦点设置在第一个文本框上,而不是将其放在复选框上。
this.Activated += new EventHandler(form_Activated);
在该方法中,我尝试将焦点设置在面板中的第一个文本框上,
private void form_Activated(object sender, EventArgs e)
{
if (this.parametersPanel.Controls.Count > 0)
{
this.parametersPanel.Focus();
(this.parametersPanel.Controls[0]).Focus();
}
}
但这不起作用,some1 可以帮助我吗?
I have a Form that has a panel with some textBoxes and checkBox that is outside the panel.
Every time the Form is loaded the checkBox has focus.
I have put en event handler when form loads and tried to set focus on first textbox instead having it on the checkbox.
this.Activated += new EventHandler(form_Activated);
in the method i try to set the focus on the first textbox in the panel
private void form_Activated(object sender, EventArgs e)
{
if (this.parametersPanel.Controls.Count > 0)
{
this.parametersPanel.Focus();
(this.parametersPanel.Controls[0]).Focus();
}
}
This does not work, can some1 help me pls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试将焦点直接设置在文本框上,而不是使用面板的控件索引。
try setting the focus directly on the textbox instead of using panel's controls index.
在设计模式下,选择您的控件并将其 tabindex 设置为 0
In desing mode, select your control and set it's tabindex to 0
选项 1:
将其放入表单的加载事件中:
选项 2:
将其放入表单的加载事件中:
Focus() 在 TextBox 可见之前不会起作用。
Option 1:
Put this in the form's load event:
Option 2:
Put this in the form's load event:
Focus() will not work until the TextBox is visible.
尝试使用
Shown
代替Activated
Instead of
Activated
tryShown
Yoc可以使用Ahmet提供的解决方案。由于您希望文本框获得焦点...将选项卡索引设置为零即可做到这一点。
您还可以使用文本框的焦点方法在表单的加载事件中设置焦点......
Yoc can use the solution provided by Ahmet. Since you want the text box to have the focus...setting the tab index to zero will do that.
Also you can use the textbox'e focus method to set the focus, in the form's load event....