2

我使用消息框来确保用户是否想从 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 在 vi​​ewTransaction 表单中显示的位置

    ///////////////////////////////
    //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,这就是它关闭我的表单的原因。

4

2 回答 2

0

我通过添加 this.dialogResult = dilaogResult.None; 解决了它 一旦 button3_Click 被调用,base.DialogResult 就因为某种原因取消了

史蒂夫,当我尝试您的线路时,它仍然会关闭,但感谢您告诉我如何看,这就是我想出来的

private void button3_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.None;
        DialogResult result = new DialogResult();
        result = MessageBox.Show("Are you sure you want to delete this element?", "Confirmation", MessageBoxButtons.YesNo);
        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
            }
        }

    }
于 2014-09-26T17:06:49.843 回答
0

如果您的button3按钮的属性DialogResult设置为不同于DialogResult.None. (查看属性窗口)

当您单击按钮时,DialogResult 属性将传递给表单的 DialogResult 属性,如果它与 None 不同,则关闭表单。
这就是模态表单如何将用户所做的选择传达给调用代码。

通常,此错误是由于Copy/Paste使用属性设计器正确设置另一个按钮控件以返回 DialogResult 造成的。顺便说一句,new DialogResult不需要您初始化 a 的代码。

所以我建议将 button3.DialogResult 属性设置为 DialogResult.None,然后,如果单击 button3 导致用户确认直接将 Form.DialogResult 属性设置为 Yes。

private void button3_Click(object sender, EventArgs e)
{
    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
        }
        this.DialogResult = DialogResult.Yes;
    }
}
于 2014-09-26T16:58:37.437 回答