2

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

tabControl1.SelectedTab = tabPage2;

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

4

2 回答 2

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

更新

检查: 如何在 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
于 2011-08-30T15:54:26.927 回答
0

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

MyUserControl userControl = new MyUserControl(tabControl1, pageIndex1);

或者

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

在这种情况下,您的 UserControl 将能够处理用户单击和切换选项卡。

Or you can create events in your UserControl that will fire when user clicks on UserControl's button. Your main form should subscribe to this events and handle them. In this case the code of your main form will be responsible for switching tabs. It's a better solution I think.

于 2011-08-30T17:07:22.737 回答