使用其中的按钮在标签页之间切换

时间:2011-08-30 15:52:43

标签: c# user-controls tabcontrol tabpage

我在表单中有一个选项卡控件,用户控件被加载到表单加载事件的每个选项卡页面上。我想使用标签页中的命令按钮在标签之间切换,这意味着按钮位于不同的用户控件中而不是表单上。

tabControl1.SelectedTab = tabPage2;
由于这个原因,

无法使用。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

tabControl1.SelectedIndex = index; 
//where index is the index (integer value) of the tabpage you want to select

<强>更新

检查: How to access properties of a usercontrol in C#

将属性公开为用户控件的属性,如下所示:

public int TabControlIndex
{
get { return tabControl1.index; }
set { tabControl1.index = value; }
 }

您可以在form load event上拨打同样的电话:

Usercontrol1.TabControlIndex = index;
//where index is the index (integer value) of the tabpage you want to select

答案 1 :(得分:0)

您可以通过构造函数或一些初始化方法将TabControl实例(及其pageIndex)作为参数传递给UserControl:

MyUserControl userControl = new MyUserControl(tabControl1, pageIndex1);

MyUserControl userControl2 = new MyUserControl();
userControl2.BindToTabControl(tabControl1, pageIndex2);

在这种情况下,您的UserControl将能够处理用户点击和切换标签。

或者您可以在UserControl中创建将在用户单击UserControl按钮时触发的事件。您的主表单应订阅此事件并处理它们。在这种情况下,主表单的代码将负责切换选项卡。我认为这是一个更好的解决方案。