我在 WinForms 应用程序中使用 MVP 监督演示者模式。我有一个带有 aTabControl
和多个TabPage
s 的视图。我需要延迟加载每个标签页的内容,因为在尝试填充整个视图时存在明显的滞后。
我希望挂钩 TabPage.GotFocus 事件并在选择给定选项卡但似乎不起作用时提供处理程序。所以我不得不使用TabControl.SelectedIndexChanged
事件和 switch 语句来让它工作。必须放入 switch 语句是一个真正的痛苦。有没有办法避免 switch 语句?
public partial class Form1 : Form
{
public event EventHandler TabPage1Selected;
public event EventHandler TabPage2Selected;
public event EventHandler TabPage3Selected;
public Form1()
{
InitializeComponent();
this.tabControl1.SelectedIndexChanged += tabControl1_SelectedIndexChanged;
this.tabPage1.GotFocus += tabPage1_GotFocus; //Doesn't work!
}
private void tabPage1_GotFocus(object sender, EventArgs e)
{
Debug.WriteLine("event fired for tabpage1");
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.WriteLine("Raising tab selected event");
EventHandler h = null;
switch (this.tabControl1.SelectedIndex)
{
case 0:
h = this.TabPage1Selected;
break;
case 1:
h = this.TabPage2Selected;
break;
case 2:
h = this.TabPage3Selected;
break;
default:
break;
}
if (h != null) h(this, new EventArgs());
}
}