我的应用程序中有一个tabforms,每当我打开多个表单时,选项卡以通常的方式打开,但每当我打开已打开的表单时,所选的选项卡都不会被更改。它会卡在打开的选项卡上last.My代码在这里。
private void Form1_MdiChildActivate(object sender, EventArgs e)
{
if (this.ActiveMdiChild == null)
tabForms.Visible = false;
// If no any child form, hide tabControl
else
{
this.ActiveMdiChild.WindowState = FormWindowState.Maximized;
// Child form always maximized
// If child form is new and no has tabPage,
// create new tabPage
if (this.ActiveMdiChild.Tag == null)
{
// Add a tabPage to tabControl with child
// form caption
TabPage tp = new TabPage(this.ActiveMdiChild.Text);
tp.Tag = this.ActiveMdiChild;
tp.Parent = tabForms;
tabForms.SelectedTab = tp;
SwapTabPages(tp);
this.ActiveMdiChild.Tag = tp;
this.ActiveMdiChild.FormClosed += new FormClosedEventHandler(ActiveMdiChild_FormClosed);
}
if (!tabForms.Visible) tabForms.Visible = true;
}
}
private void ActiveMdiChild_FormClosed(object sender, FormClosedEventArgs e)
{
//Destroy the corresponding Tabpage when closing MDI child form
if (tabForms.HasChildren)
{
((sender as Form).Tag as TabPage).Dispose();
}
//If no Tabpage left
else if (!tabForms.HasChildren)
{
tabForms.Visible = false;
}
}
private void tabForms_SelectedIndexChanged(object sender, EventArgs e)
{
if ((tabForms.SelectedTab != null) && (tabForms.SelectedTab.Tag != null))
(tabForms.SelectedTab.Tag as Form).Select();
}
答案 0 :(得分:0)
我得到了答案
else
{
for (int i = 0; i < tabForms.TabCount; i++)
{
if (tabForms.TabPages[i].Text == this.ActiveMdiChild.Text.ToString())
{
tabForms.SelectedTab = tabForms.TabPages[i];
break;
}
}
}
if (!tabForms.Visible) tabForms.Visible = true;