【发布时间】:2019-10-03 06:21:06
【问题描述】:
在我的应用程序中,我尝试在标签页中使用表单。这些表格都是数据管理表格。 我编辑了tabcontrol(创建了一个自定义tabcontrol),所以我可以通过双击tabheader来删除tabpages。
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
//* Default method of closing a tab.
if (Selectedtab != null)
TabPages.Remove(Selectedtab);
}
虽然它有效。它实际上没有做我需要的。 因为标签页中表单的任何更改都会丢失,无论以下情况如何
public partial class SomeForm : Form
{
private void SomeForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (HasChanges() && CustomMessage.WarningBox("There is unsaved data. Are you sure you want to close"))
return;
((TabControl)((TabPage)this.Parent).Parent).TabPages.Remove((TabPage)this.Parent);
}
}
在此函数设置断点时,它永远不会进入。
现在我的问题是:是否可以从 Tabcontrol 调用表单的 Close 方法。最好像下面这样。
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (Selectedtab != null)
{
if (Selectedtab.EmbeddedForm != null)
TabPages.ASelectedtab.EmbeddedForm.Close();
}
}
我面临的主要问题是我不知道如何仅知道所选选项卡来访问表单上的功能。我也找不到。
使用 KyleWangs 答案作为基础后的解决方案: 自定义控件:
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
string frmSearchName = "Frm" + SelectedTab.Name.Substring(3);
Form f = (Form)Application.OpenForms[frmSearchName];
if (f != null)
f.Close();
else
TabPages.Remove(SelectedTab);
}
表格:
private void SomeForm_FormClosing(object sender, FormClosingEventArgs e)
{
SetChanges();
if (HasChanges() && !CustomMessage.WarningBox("There is unsaved data. Are you sure you want to close?"))
e.Cancel = true;
else
((TabControl)((TabPage)this.Parent).Parent).TabPages.Remove((TabPage)this.Parent);
}
【问题讨论】:
标签: c# custom-controls tabcontrol visual-studio-2019