我有一个表格。在表格中,我可以显示一个对话框(在某些情况下),其中有文本和取消按钮。我想在表单中捕捉该按钮的事件,或者知道是否单击了取消按钮。
如何做到这一点?我相信这应该是可能的,但我搞不清楚怎么可能?
From my mainForm I have BackgroundWorker. When the backgroundWorker is started I open a childForm (with a Label and a button) and when the background task is over, I close the childForm. What I want more is : when the button of childForm is clicked the ongoing task of backgroundWorker should be cancelled. SOLUTION
在我的childForm中,我已将表单的CancelButton属性设置为cancelBtn。其他代码为:
private bool cancel;
public bool Cancel
{
get { return cancel; }
set { cancel = value; }
}
// Set the flag as true to indicate that Cancel button was actually pressed
private void cancelBtn_Click(object sender, EventArgs e)
{
Cancel = true;
}
在mainForm中:
childDlg = new ChildDialog();
// wHILE cALLING
backgroundWorker1.RunWorkerAsync();
msg = "Connecting...";
childDlg .set(msg, "");
if (!childDlg .IsAccessible)
{
// This is caught even when the dialog is closed
if (childDlg .ShowDialog() == DialogResult.Cancel) {
if (childDlg.Cancel == true) { // Was really cancelBtn pressed
// NOW ONLY do my stuff
}
}
}
我曾尝试在父类中使用@DeveloperX技术,即EventHandler,但父类方法未被调用。尝试了很多,但都没能成功。然后尝试了@RobinJ的技术,结果成功了。我只需要添加标志来识别是真的按下了取消按钮还是jjst windw正常关闭。
感谢你们所有人帮助我。我真的很感激你的帮助。
谢谢