Meh*_*hdi 3 .net c# messagebox
我在表单的FormClosing方法中放了一个Yes/No/Cancel Messagebox.现在这是消息框文本:你想保存数据吗?
如果用户点击取消按钮,我不是专业人员,也不知道如何处理?确切地说,单击取消按钮的结果必须是表单保持打开状态.
如何防止在FormClosing方法中关闭我的表单?
我写到目前为止:;)
DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);
//...
else if (dr == DialogResult.Cancel)
{
???
}
Run Code Online (Sandbox Code Playgroud)
请帮我完成我的代码!
谢谢
Mat*_*lin 12
FormClosing有一个布尔参数,如果函数返回,如果设置为True,将取消关闭表单IIRC.
编辑:例如,
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
// Set e.Cancel to Boolean true to cancel closing the form
}
Run Code Online (Sandbox Code Playgroud)
看到这里.
小智 9
实际上我认为你缺少事件处理程序,哦,即使没有偶数处理程序,你也不能转向它.您必须使用这样的事件处理程序添加事件.
private void myform_Closing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning)
if (dr == DialogResult.Cancel)
{
e.Cancel = true;
return;
}
else if (dr == DialogResult.Yes)
{
//TODO: Save
}
}
//now add a default constructor
public myform() // here use your form name.
{
this.FormClosing += new FormClosingEventHandler(myform_Closing);
}
Run Code Online (Sandbox Code Playgroud)
请原谅我,如果这段代码中有一些错误的拼写,因为我没有在c#中写它并在这里复制粘贴.我刚刚在这里写了它.:)
您可以拥有以下内容:
if(dr == DialogResult.Cancel)
{
e.Cancel = true;
}
else if(dr == DialogResult.Yes)
{
//Save the data
}
Run Code Online (Sandbox Code Playgroud)
如果您选择是或否,上述代码应仅关闭表单,并在您选择是时保存数据.
| 归档时间: |
|
| 查看次数: |
39287 次 |
| 最近记录: |