问题描述
我调用自定义表单如下:
I call up a custom form as follows:
SomeCustomForm _newForm = new SomeCustomForm();
_newForm.ShowDialog();
//**SOME OTHER CODE**
现在假设我们有一些自定义事件(我的在 DataGridView DoubleClick 上):
Now lets say we have some custom event (Mine is on a DataGridView DoubleClick):
private void dgvSomeGrid_DoubleClick(object sender, EventArgs e)
{
string name = dgvSomeGrid.CurrentRow.Cells[5].Value.ToString();
DialogResult = MessageBox.Show(name, "Select this Merkmal?", MessageBoxButtons.YesNo);
if (DialogResult == DialogResult.Yes)
{
_someID = Convert.ToInt32(dgvMSomeGrid.CurrentRow.Cells[0].Value.ToString());
this.Close();
}
else if (DialogResult == DialogResult.No)
{
return;
}
}
对话框工作正常,因为 no 和 yes 按钮的行为符合预期.但我的问题是,无论单击哪个按钮,代码都会跳回到 //**SOMEOTHERCODE.所以实际上,_newForm 只是关闭了.
The dialog works fine in that the no and yes buttons behave as expected. My problem though is that irrespective of which button is clicked, the code jumps back to //**SOMEOTHERCODE. So in effect, the _newForm is just closed.
我显然不希望这种情况发生,因为如果选择否",我还没有完成另一种形式的工作.按钮被点击.
I obviously don't want this to happen as I am not done on the other form yet if the "No" button is clicked.
有什么帮助吗?
我很抱歉 - 为了清楚起见.上面提到的网格在_newForm 上.并且对话框是从 _newForm 调用的.
My apologies - for the sake of clarity. The grid mentioned above is on the _newForm. And the dialog is called from the _newForm.
这会意外关闭.
推荐答案
不要使用表单的 DialogResult
属性进行比较.仅在成功关闭时设置
don't use DialogResult
property of form for comparison. Set it only on successful close
private void dgvSomeGrid_DoubleClick(object sender, EventArgs e)
{
string name = dgvSomeGrid.CurrentRow.Cells[5].Value.ToString();
var result = MessageBox.Show(name, "Select this Merkmal?", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
_someID = Convert.ToInt32(dgvMSomeGrid.CurrentRow.Cells[0].Value.ToString());
this.DialogResult = DialogResult.Yes;
this.Close();
}
}
这篇关于内置 MessageBox 对话框关闭自定义 Form.ShowDialog()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!