如何为表单X(在最右上方)和关闭按钮创建相同的功能.这两个需要表现相似这就是我在btnClose_Click中所拥有的
private void btnClose_Click(object sender, EventArgs e)
{
DialogResult result;
int fileId = StaticClass.FileGlobal;
if (DataDirty)
{
string messageBoxText = "You have unsaved data. Do you want to save the changes and exit the form?";
MessageBoxButtons button = MessageBoxButtons.YesNo;
string caption = "Data Changed";
MessageBoxIcon icon = MessageBoxIcon.Question;
result = MessageBox.Show(messageBoxText, caption, button, icon);
if (result == DialogResult.No)
{
Program.fInput = new frmInputFiles(gtId, gName);
Program.fInput.Show();
this.Close();
}
if (result == DialogResult.Yes)
{
return;
}
}
else
{
Program.fInput = new frmInputFiles(gPlantId, gPlantName);
Program.fInput.Show();
this.Close();
}
}
Even on clicking the X to close the form,it should behave the same way as btnClose_Click
private void frmData_FormClosing(object sender, FormClosingEventArgs e)
{
btnClose_Click(sender,e);//this doesnt seem to be working.
}
Run Code Online (Sandbox Code Playgroud)
它正在无限循环中.我明白了它是这样做的... btnClose_Click()有this.Close()调用frmData_FormClosing ..其中调用btnclose ..
感谢你
只需将this.Close()放在btnClose_Click()事件中.然后将所有剩余的逻辑(您需要将其编辑)移动到frmData_FormClosing()事件中并调用,e.Cancel = true;如果要取消关闭表单(在您的情况下,如果有未保存的更改并且用户单击是提示.
这是一个例子(我只是剪切并粘贴在记事本中,所以公平警告):
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmData_FormClosing(object sender, FormClosingEventArgs e)
{
if (DataDirty)
{
if (MessageBox.Show("You have unsaved data. Do you want to save the changes and exit the form?",
"Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Program.fInput = new frmInputFiles(gtId, gName);
Program.fInput.Show();
}
else
e.Cancel = true;
}
else
{
Program.fInput = new frmInputFiles(gPlantId, gPlantName);
Program.fInput.Show();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1934 次 |
| 最近记录: |