我有一個簡單的winforms應用程序。應用程序始終始終隱藏着MainForm。 MainForm打開另一個稱爲Notification(.Show)的表單,該表單在10秒後關閉。因此,如果MainForm在Notification仍處於打開狀態時打開對話框窗體(.ShowDialog),則當Notification點擊10秒並關閉時,打開的對話框也會關閉。Form.Close()也關閉其他窗體(對話框)
通知形式以及對話形式具有TopMost = True
一個例子:
短的例子: 的OtherDialogForm關閉也即使我的應用程序不僅會:
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
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
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;
}
}
}
我真的不知道爲什麼對話與通知一起關閉。
一個對話框,需要一個所有者窗口被關閉。如果你沒有提供一個ShowDialog(所有者)重載或所有者屬性,那麼它自己去尋找一個。它將選擇由同一個線程擁有的活動窗口。如果關閉或隱藏該窗口,那麼該對話框也是歷史記錄。注意它不會是MainForm,即使你在其構造函數中調用了ShowDialog(),它仍然不活躍。否則通過使用ShowDialog(this)瑣碎修復。 –