使用标签页中的按钮在标签页之间切换
我在表单中有一个选项卡控件,并且用户控件在表单加载事件中加载到每个选项卡页上。我想使用选项卡页中的命令按钮在选项卡之间切换,这意味着这些按钮位于不同的用户控件中,而不是在表单上。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新
检查:
如何在 C# 中访问用户控件的属性
属性作为用户控件的属性,如下所示:
您可以在
表单加载事件
上调用相同的属性,如下所示:UPDATE
Check:
How to access properties of a usercontrol in C#
Expose the properties as properties of your user control like this:
you can call the same on your
form load event
like this:您可以通过构造函数或某些初始化方法将 TabControl 实例(及其 pageIndex)作为参数传递给 UserControl:
或者
在这种情况下,您的 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:
or
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.