简体   繁体   English

Winform-SetFocus到TabControl上usercontrol内的文本框

[英]Winform - SetFocus to textbox inside usercontrol on TabControl

Winform application Multiple tabs on the tabs are replicated Usercontrol Winform应用程序复制选项卡上的多个选项卡

When clicking on a particular tab, I would like to setfocus to a textbox within the usercontrol. 单击特定选项卡时,我想将焦点设置到usercontrol中的文本框。 I would like to this if possible from within the tabControl_SelectedIndexChanged event. 我想从tabControl_SelectedIndexChanged事件中做到这一点。

ex: 例如:

textbox name = txtOne 文本框名称= txtOne
txtOne resides within UserControlA txtOne驻留在UserControlA中
UserControlA resides within tabControl.SelectedTab.Text = "Tab2" UserControlA驻留在tabControl.SelectedTab.Text =“ Tab2”中

When I click Tab2 I'd like Focus to be set to txtone. 当我单击Tab2时,我希望将Focus设置为txtone。

I've tried: (and many other things!) UserControlA.Controls["txtone"].SelectAll(); 我试过了:(还有很多其他事情!)UserControlA.Controls [“ txtone”]。SelectAll(); - returns object reference not set to an instance of an object -返回未设置为对象实例的对象引用

Thanks! 谢谢!

Because txtone is not an immediate child of UserControlA , UserControlA.Controls["txtone"] will return null. 因为txtone不是UserControlA的直接子代,所以UserControlA.Controls["txtone"]将返回null。

You can either dig your way down the control hierarchy with control names ( UserControlA.Controls["fameFD"].Controls["txtone"] ) or simply expose txtone as a public field or property in your UserControl class. 您可以使用控件名称( UserControlA.Controls["fameFD"].Controls["txtone"] )在控件层次结构中进行深入研究,也可以将txtone作为公共字段或UserControl类中的属性公开。 The latter would look something like this: 后者看起来像这样:

public TextBox TxtOne {
    get
    {
        return txtone;
    }
}

Then you could refer to the textbox from external code like this: 然后,您可以像这样从外部代码引用文本框:

UserControlA.TxtOne.SelectAll();

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

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