我使用c#开发了一个Windows窗体应用程序。
它有一个标签控件和一个菜单栏。我想在单击菜单项时访问标签页中的控件。
例如我的标签控件有5个标签页,菜单栏有5个菜单项。 单击菜单项1时,我想访问标签页1中的文本框。
怎么做?
答案 0 :(得分:0)
代码:
tabControlName.SelectedIndex = theIndexOfTheTabPage; //switch to the tab page
tabControl1.TabPages[theIndexOfTheTabPage].Controls.Find("textBoxName", true)[0].Select(); //find the TextBox and select it
通过更改SelectedIndex
的{{1}}属性,第一行更改为所需的标签页。第二行使用tabControl
方法搜索TextBox
。然后,使用Find(string name, bool searchAllChildren)
方法重点关注TextBox
。
要单击选项卡内的按钮,请使用以下代码:
Select()
首先以与tabControlName.SelectedIndex = theIndexOfTheTabPage; //switch to the tab page
Button b = tabControlName.TabPages[theIndexOfTheTabPage].Controls.Find("buttonName", true)[0] as Button;
b.PerformClick();
相同的方式获取Button
。然后使用TextBox
单击按钮