【发布时间】:2014-09-26 16:46:45
【问题描述】:
我使用消息框来确定用户是否想从 gridview 中删除一行但是无论他们给出什么答案都会关闭表单并返回到 form1
这是加载 viewTransactions 表单的地方,这段代码在 Form1 中
private void btnViewTrans_Click(object sender, EventArgs e)
{
viewTransactions = new View_Transactions(newList);
if (viewTransactions.ShowDialog() == DialogResult.OK)
{
newList.Equals(viewTransactions.getList());
}
}
这是在 viewTransaction 表单中显示 messageBox 的地方
///////////////////////////////
//Remove an item from the list
private void button3_Click(object sender, EventArgs e)
{
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to delete this element?", "Confirmation", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes)
{
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
tmpList.remove(item.Index);//remove item from tmpList used to update the passed in list
dataGridView1.Rows.RemoveAt(item.Index);//remove the item from the dataGrid
}
}
}
在我使用消息框显示警告之前,我的代码没有问题。我相信 DialogResult 正在传递给另一个 ShowDialog,这就是它关闭我的表单的原因。
【问题讨论】:
-
您可能在从当前正在迭代的集合中删除时遇到问题。这通常会导致问题。
-
查看 button3 控件的属性 DialogResult 的值。如果它设置为 None 以外的任何值,您的表单将关闭。
-
就像我说的,在放入消息框之前我没有任何问题。删除元素并重新复制列表工作正常。我得到的最大错误是当表单关闭并且它带我回到 form1 时,newList.Equals 没有运行,因为 dialogResult 是是或否。
-
史蒂夫这是怎么回事,但为什么呢? dialogResult 不应该特定于被调用的消息框吗?就像我的消息框不应该有一个与 viewTransaction 表单的 dialogResult 不同的 dialogResult
标签: c# winforms forms messagebox dialogresult