Winforms c# - 将焦点设置为 TabPage 的第一个子控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Winforms c# - Set focus to first child control of TabPage
提问by Corin Blaikie
Say I have a Textbox
nested within a TabControl
.
假设我有一个Textbox
嵌套在TabControl
.
When the form loads, I would like to focus on that Textbox
(by default the focus is set to the TabControl
).
当表单加载时,我想关注它Textbox
(默认情况下,焦点设置为TabControl
)。
Simply calling textbox1.focus()
in the Load
event of the form does not appear to work.
简单地textbox1.focus()
在Load
表单的事件中调用似乎不起作用。
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;
}
}
My question is:
我的问题是:
Is there a more elegant way to do this?
有没有更优雅的方法来做到这一点?
采纳答案by samjudson
The following is the solution:
以下是解决方案:
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.
然而,更好的问题是为什么......我不完全确定那个答案是什么。
Edit: I suspect it is something to do with the fact that both the form, and the TabControl are containers, but I'm not sure.
编辑:我怀疑这与表单和 TabControl 都是容器的事实有关,但我不确定。
回答by Korby
Try putting it in the Form_Shown()
event. Because it's in a container, putting in the Form_Load or even the Form() constructor won't work.
尝试将其放入Form_Shown()
事件中。因为它在容器中,所以放入 Form_Load 甚至 Form() 构造函数都不起作用。
回答by bdwakefield
You just need to add the Control.Select() for your control to this code. I have used this to set focus on controls during validation when there are errors.
您只需要将控件的 Control.Select() 添加到此代码中即可。当出现错误时,我已经使用它在验证期间将焦点设置在控件上。
private void ShowControlTab(Control ControlToShow)
{
if (!TabSelected)
{
if (ControlToShow.Parent != null)
{
if (ControlToShow.Parent.GetType() == typeof(TabPage))
{
TabPage Tab = (TabPage)ControlToShow.Parent;
if (WOTabs.TabPages.Contains(Tab))
{
WOTabs.SelectedTab = Tab;
TabSelected = true;
return;
}
}
ShowControlTab(ControlToShow.Parent);
}
}
}
回答by Mikhail G
Try to use textbox1.Select()
instead of textbox1.Focus()
. This helped me few times.
尝试使用textbox1.Select()
而不是textbox1.Focus()
. 这帮助了我几次。
回答by Reh
I had a user control within another user control. textbox1.Select() worked for me but textbox1.Focus() did not work.
我在另一个用户控件中有一个用户控件。textbox1.Select() 对我有用,但 textbox1.Focus() 不起作用。
You can also try setting Tabstop to false, textbox1.Focus(), TabStop true.
您也可以尝试将 Tabstop 设置为 false、textbox1.Focus()、TabStop 为 true。
回答by user3511799
private void ChildForm1_Load(object sender, EventArgs e)
{
ActiveControl = txt_fname;
}
i use this code it works fine on win tab control or dotnetbar supertab contrl
我使用此代码它在 win tab control 或 dotnetbar supertab contrl 上工作正常