Form.Close()也关闭其他表单(对话框) [英] Form.Close() is closing other form (dialog) also

查看:69
本文介绍了Form.Close()也关闭其他表单(对话框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的winforms应用程序.该应用程序从MainForm一直隐藏着. MainForm将打开另一个名为Notification(.Show)的窗体,该窗体将在10秒钟后关闭.因此,如果在Notification仍处于打开状态时MainForm打开一个对话框窗体(.ShowDialog),则如果Notification命中10秒并关闭,则打开的对话框也会关闭.

I have a simple winforms application. The application starts with MainForm which is hidden all the time. MainForm opens another form called Notification (.Show) which closes after 10 seconds. So if the MainForm opens a dialog form (.ShowDialog) while Notification is still open, the open dialog also closes if Notification hits 10 seconds and closes.

通知表单和对话框表单具有TopMost = True

The Notification form as well as the dialog form have TopMost = True

一个例子:

简短示例: 即使我的应用程序仅执行此操作,OtherDialogForm也会关闭:

Short example: The OtherDialogForm closes also even if my application only does this:

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        new Notification("Title", "Content").Show(); // closes itself after 10 seconds
        new OtherDialogForm().ShowDialog(); // being closed by form above (not wanted)
    }
}

更长的例子: MainForm.cs

Longer example: MainForm.cs

public partial class MainForm : Form
{
    protected override void SetVisibleCore(bool value)
    {
        base.SetVisibleCore(false);
    }

    public MainForm() 
    {
        CreateNotification();
    }

    public void CreateNotification()
    {
        new Notification.Show();
    }

    // triggered while Notification (from above) is still open
    private async void TriggeredByHotkey()
    {
        using (OtherDialogForm dialog = new OtherDialogForm())
        {
            dialog.ShowDialog();
            // if DialogResult == ...
        }
    }
}

Notification.cs

Notification.cs

public partial class Notification : Form
{
    Timer timer;

    public Notification()
    {
        // sets timer tick to 10 seconds,
        // then calls void with this.Close()
    }

    // not sure if I really need this, but the problem is still there without
    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            cp.ExStyle |= 8;  // Turn on WS_EX_TOPMOST
            return cp;
        }
    }
}

我真的不知道为什么对话框和通知一起关闭.

I have really no idea why the dialog closes together with Notification.

推荐答案

您可以隐藏通知"表单而不是将其关闭,因为这只是一个通知,您并不需要将其关闭,因此可以将其保留在后台,在需要时显示它. 在msdn中: https://msdn.microsoft. com/en-us/library/c7ykbedk(v = vs.110).aspx

You can hide the Notification form instead of closing it, since is just a notification you do not really need to close it, you can keep it in the background and show it when is needed. From the msdn: https://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.110).aspx

此版本的ShowDialog方法未将窗体或控件指定为其所有者.调用此版本时,将使当前活动的窗口成为对话框的所有者.如果要指定特定所有者,请使用此方法的其他版本.

This version of the ShowDialog method does not specify a form or control as its owner. When this version is called, the currently active window is made the owner of the dialog box. If you want to specify a specific owner, use the other version of this method.

因此,如果您使用的是ShowDialog,并且Notification表单成为另一个Dialig表单的父表单,则它们都将在关闭Notification表单时关闭

So if you are using ShowDialog and the Notification form becomes the parent of the other dialig form, they both will be closed at closing Notification form

这篇关于Form.Close()也关闭其他表单(对话框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆