简体   繁体   English

CF - 将焦点设置为特定控件

[英]CF - Set Focus to a specific control

I have a Form that has a panel with some textBoxes and checkBox that is outside the panel. 我有一个Form,其中有一个面板,其中包含一些textBox和checkBox,它位于面板之外。 Every time the Form is loaded the checkBox has focus. 每次加载表单时,checkBox都有焦点。

I have put en event handler when form loads and tried to set focus on first textbox instead having it on the checkbox. 我在表单加载时放了en事件处理程序,并尝试将焦点设置在第一个文本框上,而不是在复选框上。

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? 这不起作用,有些人可以帮助我吗?

Option 1: 选项1:

Put this in the form's load event: 把它放在表单的load事件中:

this.ActiveControl = myTextBox;

Option 2: 选项2:

Put this in the form's load event: 把它放在表单的load事件中:

this.Show();
myTextBox.Focus();

Focus() will not work until the TextBox is visible. 在TextBox可见之前,Focus()将无效。

尝试直接在文本框上设置焦点,而不是使用面板的控件索引。

在设计模式下,选择您的控件并将其tabindex设置为0

而不是Activated尝试Shown

Yoc can use the solution provided by Ahmet. Yoc可以使用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.... 你也可以使用textbox'e focus方法设置焦点,在表单的加载事件....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM