的WinForms C# - 设置焦点的TabPage的第一子控件控件、焦点、WinForms、TabPage

2023-09-02 02:10:11 作者:做你眼里的星星

说我有一个文本框嵌套在的TabControl

当加载窗体,我想重点放在文本框(默认焦点设置为的TabControl )。

When the form loads, I would like to focus on that Textbox (by default the focus is set to the TabControl).

简单地调用 textbox1.focus()加载表格的事件似乎并没有工作。

Simply calling textbox1.focus() in the Load event of the form does not appear to work.

我已经能够通过执行以下操作重点是:

I have been able to focus it by doing the following:

 private void frmMainLoad(object sender, EventArgs e)
 {
     foreach (TabPage tab in this.tabControl1.TabPages) 
     {
         this.tabControl1.SelectedTab = tab;
     }
 }

我的问题是:

有没有更优雅的方式来做到这一点?

Is there a more elegant way to do this?

推荐答案

下面是解决方案:

private void frmMainLoad(object sender, EventArgs e)
{
    ActiveControl = textBox1;
}

最好的问题将然而是为什么......我不完全知道这个问题的答案之一。

The better question would however be why... I'm not entirely sure what the answer to that one is.

编辑:我怀疑这是值得做的事实,无论是形式,和的TabControl是容器,但我不知道

I suspect it is something to do with the fact that both the form, and the TabControl are containers, but I'm not sure.