简体   繁体   中英

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. 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?

Option 1:

Put this in the form's load event:

this.ActiveControl = myTextBox;

Option 2:

Put this in the form's load event:

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

Focus() will not work until the TextBox is visible.

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

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

而不是Activated尝试Shown

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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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