我使用消息框来确保用户是否想从 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());
}
}
这是 messageBox 在 viewTransaction 表单中显示的位置
///////////////////////////////
//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,这就是它关闭我的表单的原因。