我最近创建了一个简单的自定义消息框。它只是使用 ShowDialog() 调用的另一种形式。
我有两个按钮 YES / NO 来设置 DialogResult 值,然后使用 this.Hide() 隐藏表单。
但是,当我这样做时,整个应用程序都会关闭。使用 this.Close() 时不会发生这种情况。我选择使用隐藏的原因是因为响应似乎更快。使用关闭时,消息框窗体会在关闭前停留 2-3 秒。
下面是一些代码:
public static void Init()
{
if (_instance == null)
{
_instance = new MQMessageBox();
_instance.MQButtonYes.Click += MQButtonYes_Click;
_instance.MQButtonNo.Click += MQButtonNo_Click;
}
}
public static DialogResult Show(string caption, string message)
{
Init();
_instance.Caption = caption;
_instance.Message = message;
DialogResult result = _instance.ShowDialog();
return result;
}
private void MQButtonYes_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Yes;
this.Hide();
}
private void MQButtonNo_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.No;
this.Hide();
}
从主要形式,它被称为:
MQMessageBox.Show("Warning", "Hello World");
this.Show(); //Adding this call, will show the main form again. Without this call, the mobile will show the Today Screen making it appear the app has crashed.
主要方法是:
MQMainForm mainForm = new MQMainForm();
Application.Run(mainForm);