C# 标签页点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9684221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TabPage Click Events
提问by Rob
I'm trying to automatically trigger events based on the tab page that is clicked on the tab control.
我正在尝试根据在选项卡控件上单击的选项卡页自动触发事件。
In design mode of my form, when I click on the tabs the properties box says Tabs System.Windows.Forms.TabControl whichever tab is selected. However, I have to click on the actual page, not the tab for the property to change to the name of the pages e.g. TaskListPage System.Windows.Forms.TabPage.
在我的表单的设计模式中,当我单击选项卡时,属性框显示 Tabs System.Windows.Forms.TabControl 选择了哪个选项卡。但是,我必须单击实际页面,而不是属性选项卡才能更改为页面名称,例如 TaskListPage System.Windows.Forms.TabPage。
My tabcontrol is called Tabs and I was trying to test it out using the code below which is supposed to display a message based on the tab option.
我的 tabcontrol 被称为 Tabs,我试图使用下面的代码对其进行测试,该代码应该根据选项卡选项显示一条消息。
private void Tabs_SelectedIndexChanged(object sender, EventArgs e)
{
if (Tabs.SelectedTab == TaskListPage)
{
MessageBox.Show("TASK LIST PAGE");
}
else if (Tabs.SelectedTab == SchedulePage)
{
MessageBox.Show("SCHEDULE PAGE");
}
}
When I test the code above, nothing is happening.
当我测试上面的代码时,什么也没有发生。
Any help in getting the events working when a specific tab is clicked would be greatly appreciated!
在单击特定选项卡时让事件工作的任何帮助将不胜感激!
Thankyou
谢谢
采纳答案by LarsTech
It sounds like you don't have it wired up:
听起来你没有连接它:
public Form1() {
InitializeComponent();
Tabs.SelectedIndexChanged += new EventHandler(Tabs_SelectedIndexChanged);
}
There are other events that can give you this information as well: Selectedand Selecting.
还有其他事件也可以为您提供此信息:Selected和Selecting。
void Tabs_Selected(object sender, TabControlEventArgs e) {
if (e.TabPage == TaskListPage) {
// etc
}
}
回答by Larryrl
This first part goes in the
第一部分在
public Form1()
{
// This is near the top of the form 1 code in form1.cs
InitializeComponent();
tabControl1.SelectedIndexChanged += new EventHandler(TabControl1_SelectedIndexChanged);
}
Then down below in your regular code you simply tell which control should have the focus after the tab page is clicked. Which in my word processor, I used a rich text box, and tab controls to simulate the msword ribbon. The rich text control in my case is not on a tab page as my tab pages cover maybe 1 or 2 inch on the top of the form
然后在下面的常规代码中,您只需告诉单击选项卡页后哪个控件应该具有焦点。在我的文字处理器中,我使用了富文本框和选项卡控件来模拟 msword 功能区。我的情况下的富文本控件不在标签页上,因为我的标签页可能覆盖表单顶部的 1 或 2 英寸
private void TabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
richTextBox1.Focus();
}
This is what I call my word processor. It's here for anyone that would like to use it. Larry's Journal
这就是我所说的文字处理器。任何想要使用它的人都可以使用它。拉里的日记
回答by digisoft
private void tabControl1_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedTab.Text =="All")
{
MessageBox.Show("All");
}
else
MessageBox.Show("hello world");
}
}
Try This It should be work

