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

发布于 2024-12-01 21:41:38 字数 186 浏览 7 评论 0原文

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

tabControl1.SelectedTab = tabPage2;

因此无法使用。我怎样才能实现这个目标?

I have a tab control in a form and user controls are loaded onto each tab page at the form load event. I want to switch between tabs using command buttons within the tab pages, meaning the buttons are in different user controls and not on the form.

tabControl1.SelectedTab = tabPage2;

could not be used because of that. How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗失的美好 2024-12-08 21:41:39
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; }
 }

您可以在表单加载事件上调用相同的属性,如下所示:

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

UPDATE

Check:
How to access properties of a usercontrol in C#

Expose the properties as properties of your user control like this:

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

you can call the same on your form load event like this:

Usercontrol1.TabControlIndex = index;
//where index is the index (integer value) of the tabpage you want to select
喵星人汪星人 2024-12-08 21:41:39

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

MyUserControl userControl = new MyUserControl(tabControl1, pageIndex1);

或者

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

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

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

You can pass TabControl instance (along with its pageIndex) to your UserControl as a parameter either via constructor or some initializer method:

MyUserControl userControl = new MyUserControl(tabControl1, pageIndex1);

or

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

In this case your UserControl will be able to handle user clicks and switch tabs.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文