我有一个具有onw窗体的Windows应用程序。当我关闭表单时,我想添加一个确认框,询问"您要关闭此表单吗?"我该如何展示"不要再问我了#34;在那确认c#中的弹出窗口?
答案 0 :(得分:1)
首先,您需要Class
Solution
Conformation
static
_askUserAgain
_yes
个_no
个公共属性public class Confirmation
{
public static bool _askUserAgain = true;
public static bool _yes = false;
public static bool _no = false;
}
,FormClosing
和private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Confirmation._askUserAgain == true)
{
e.Cancel = true;
Form2 form2 = new Form2();
form2.Show();
}
else
{
e.Cancel = false;
}
}
,用于记住用户首选项。
Fomr2
接下来将是您的Form1的 private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Confirmation._askUserAgain = false;
}
private void Yes_Click(object sender, EventArgs e)
{
Confirmation._yes = true;
Confirmation._no = false;
Form1 parentForm = Application.OpenForms["Form1"] as Form1;
if (parentForm != null)
{
parentForm.Hide();
}
this.Hide();
}
private void No_Click(object sender, EventArgs e)
{
Confirmation._no = true;
Confirmation._yes = false;
if (Confirmation._no)
{
this.Hide();
Form1 parentForm = Application.OpenForms["Form1"] as Form1;
parentForm.Show();
}
}
事件
{{1}}
下一个{{1}}内显示:
{{1}}
答案 1 :(得分:0)
创建一个带有标签,复选框和OK按钮的表单。
每当此表单关闭时:(单击OK按钮);确保没有其他可能的方式来关闭此表单
private void OK_Click(object sender, eventArgs e)
{
DoCheck();
DialogResult = DialogResult.OK; //this closes your form
}
private void DoCheck()
{
if(checkBox1.Checked)
{
//Set your value on true
MyValues.Confirm = true;
}
}