C#关闭Winform选项卡式子窗体 [英] C# Closing Winform Tabbed Child Form
问题描述
您好,
我在关闭子表格时遇到了一些问题。
I'm having some problems making my Child Form being closed.
我有一个名为MdiParent的表单BackOffice有一个TreeView。当我点击一个节点时,它会打开一个新的表格"Contratos_List"。并为该表单创建一个Tab。到目前为止,非常好。
I've a MdiParent Form named BackOffice which has a TreeView. When I click on a node, it opens a new Form "Contratos_List" and creates a Tab for that form. So far, so Good.
当我尝试关闭Contratos_List时,按钮什么都不做。我一遍又一遍地搜索了很多论坛,但可以"弄清楚我的代码有什么问题。"
When I try to close the Contratos_List, the button does nothing. I searched over and over many forums, but can' figure what's wrong with my code.
所以,这就是我到目前为止所做的一切。
So, here's what I have so far.
这个是我的BackOffice表单代码:
This is my BackOffice Form code:
private void Backoffice_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
int tpIndex = 0;
foreach (TabPage tpCheck in tabForms.TabPages)
{
if (tpCheck.Name == this.ActiveMdiChild.Name)
{
tabForms.SelectedIndex = tpIndex;
return;
}
tpIndex++;
}
TabPage tp = new TabPage(this.ActiveMdiChild.Text);
tp.Tag = this.ActiveMdiChild;
tp.Parent = tabForms;
tabForms.SelectedTab = tp;
this.ActiveMdiChild.Tag = tp;
this.ActiveMdiChild.FormClosed += new FormClosedEventHandler(ActiveMdiChild_FormClosed);
}
else
{
TabPage tp = this.ActiveMdiChild.Tag as TabPage;
if (tp != null)
tabForms.SelectedTab = tp;
}
if (!tabForms.Visible) tabForms.Visible = true;
}
}
private void tabForms_SelectedIndexChanged(object sender,
EventArgs e)
{
if ((tabForms.SelectedTab != null) &&
(tabForms.SelectedTab.Tag != null))
(tabForms.SelectedTab.Tag as Form).Select();
}
private void ActiveMdiChild_FormClosed(object sender,
FormClosedEventArgs e)
{
((sender as Form).Tag as TabPage).Dispose();
}
private void padmintreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Name == "contratos_node")
{
if (Application.OpenForms.OfType<Operacoes.Contratos.Contratos_List>().FirstOrDefault() != null)
{
return;
}
Operacoes.Contratos.Contratos_List ct = new Operacoes.Contratos.Contratos_List { MdiParent = this, WindowState = FormWindowState.Maximized };
ct.Show();
}
}
然后,我有Contratos_List表单关闭按钮:
Then, I have the Contratos_List Form close button:
private bool ChildWindowOpen(String childWindow)
{
foreach (Form f in this.MdiChildren)
{
if (f.Name == childWindow)
return true;
}
return false;
}
private void btn_fechar_Click(object sender, EventArgs e)
{
if (ChildWindowOpen("Contratos_List") == false)
{
if (this.ActiveMdiChild != null)
this.ActiveMdiChild.Close();
}
}
然后,无论我如何点击关闭按钮,子表单都不会关闭。
Then, no matters how I click on Close Button, the Child Form does not close.
她是BreakPoint的标题,显示ActiveMdiChild为空。
Her's a caption from a BreakPoint showing that ActiveMdiChild is null.
所以,如果有人能在这里帮助我,那会很棒,因为我不知道在哪里可以找到答案...
So, if anyone could help me here, it would be great because I don't know where to find some answers...
谢谢。
推荐答案
CarlosFerreira,
Hi CarlosFerreira,
> ;> 当我尝试关闭Contratos_List时,该按钮不执行任何操作
我认为您使用以下代码关闭名为" Contratos_List ":
I think you use the following code to close the form named "Contratos_List":
private void btn_fechar_Click(object sender, EventArgs e)
{
if (ChildWindowOpen("Contratos_List") == false)
{
if (this.ActiveMdiChild != null)
this.ActiveMdiChild.Close();
}
}
请在此行设置一个断点:
Please set a break point in this line:
if (this.ActiveMdiChild != null)
确定是否"this.ActiveMdiChild"是null,我担心它是null,所以你不能触发" this.ActiveMdiChild.Close()"方法。
To determine if "this.ActiveMdiChild" is null, I'm afraid it is null so that you can not trigger "this.ActiveMdiChild.Close()" method.
实际上,如果要关闭名为" Contratos_List "的MDI子窗体,只需使用:
In fact, if you want to close the MDI child form named "Contratos_List", you just need to use:
//this.ActiveMdiChild.Close();
this.Close();
问候,
Frankie
这篇关于C#关闭Winform选项卡式子窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!