如何防止表单对象在关闭时被丢弃?

发布于 2024-11-08 19:01:15 字数 74 浏览 0 评论 0原文

我正在使用 MDIParent 表单。当我关闭它的孩子时,孩子的对象就会被释放。有没有办法将子级可见性设置为 false 而不是处置?

I am using an MDIParent Form. When I close its child, the object of the child disposes. Is there a way to set child visibility to false instead of disposing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

辞旧 2024-11-15 19:01:15

默认情况下,当您关闭表单时,它将被处理。您必须重写 Closing 事件来阻止它,例如:

// Use this event handler for the FormClosing event.
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
  this.Hide();
  e.Cancel = true; // this cancels the close event.
}

记住添加方法,例如在表单 ctor 中:

public MyForm()
{
    InitializeComponent();

    this.FormClosing += MyForm_FormClosing;
}

By default, when you close a form, it will be disposed. You have to override the Closing event to prevent it, for example:

// Use this event handler for the FormClosing event.
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
  this.Hide();
  e.Cancel = true; // this cancels the close event.
}

Remember to add the method, e.g. in the form ctor:

public MyForm()
{
    InitializeComponent();

    this.FormClosing += MyForm_FormClosing;
}
何处潇湘 2024-11-15 19:01:15

您可以取消关闭事件并隐藏。

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
        this.Hide();
    }

You can cancel the close event and hide instead.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
        this.Hide();
    }
烂人 2024-11-15 19:01:15

是的。您可以调用表单的“隐藏”方法。

您还可以重写 OnClosed 并且不调用其基本实现;但是,当您确实想要处理该表格时,这可能会妨碍您。

Yes. You can call the form's "Hide" method.

You can also override OnClosed and not call its base implementation; HOWEVER, when you DO want to dispose of the form, this may get in your way.

葬シ愛 2024-11-15 19:01:15

当然,您可以取消关闭并隐藏它。这看起来不是一件好事,但你绝对可以。

请参阅Form.FormClosing 事件(MSDN)。

Sure, you can cancel the close and hide it. It doesn't seem like a good thing to do, but you definitely can.

See Form.FormClosing Event (MSDN).

从来不烧饼 2024-11-15 19:01:15
    void SaveInfo()
{
blnCanCloseForm = false;
Vosol[] vs = getAdd2DBVosol();
if (DGError.RowCount > 0)
return;

Thread myThread = new Thread(() =>
{
this.Invoke((MethodInvoker)delegate {
    picLoad.Visible = true;
    lblProcces.Text = "Saving ...";
});
int intError = setAdd2DBVsosol(vs);
Action action = (() =>
{
    if (intError > 0)
    {
        objVosolError = objVosolError.Where(c => c != null).ToArray();
        DGError.DataSource = objVosolError;// dtErrorDup.DefaultView;
        DGError.Refresh();
        DGError.Show();
        lblMSG.Text = "Check Errors...";
    }
    else
    {
        MessageBox.Show("Saved All Records...");
        blnCanCloseForm = true;
        this.DialogResult = DialogResult.OK;
        this.Close();
    }

});
this.Invoke((MethodInvoker)delegate {
    picLoad.Visible = false;
    lblProcces.Text = "";
});
this.BeginInvoke(action);
});
myThread.Start();
}

void frmExcellImportInfo_FormClosing(object s, FormClosingEventArgs e)
{
    if (!blnCanCloseForm)
        e.Cancel = true;
}
    void SaveInfo()
{
blnCanCloseForm = false;
Vosol[] vs = getAdd2DBVosol();
if (DGError.RowCount > 0)
return;

Thread myThread = new Thread(() =>
{
this.Invoke((MethodInvoker)delegate {
    picLoad.Visible = true;
    lblProcces.Text = "Saving ...";
});
int intError = setAdd2DBVsosol(vs);
Action action = (() =>
{
    if (intError > 0)
    {
        objVosolError = objVosolError.Where(c => c != null).ToArray();
        DGError.DataSource = objVosolError;// dtErrorDup.DefaultView;
        DGError.Refresh();
        DGError.Show();
        lblMSG.Text = "Check Errors...";
    }
    else
    {
        MessageBox.Show("Saved All Records...");
        blnCanCloseForm = true;
        this.DialogResult = DialogResult.OK;
        this.Close();
    }

});
this.Invoke((MethodInvoker)delegate {
    picLoad.Visible = false;
    lblProcces.Text = "";
});
this.BeginInvoke(action);
});
myThread.Start();
}

void frmExcellImportInfo_FormClosing(object s, FormClosingEventArgs e)
{
    if (!blnCanCloseForm)
        e.Cancel = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文