【问题标题】:Message box closing form erradically消息框关闭窗体不规律
【发布时间】: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


【解决方案1】:

我通过添加 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
            }
        }

    }

【讨论】:

  • 你能告诉我button3上的属性DialogResult的值是多少吗?在设计器模式下单击按钮,然后查看属性窗口。
  • 当 button3_Click 被调用时,button3 的 DialogResult 是取消并且总是取消但 if 语句工作正常
  • 请看属性窗口,上面写着什么?
  • 属性窗口显示取消
  • 现在重新阅读我的答案。您将 button3 设置为取消。这个值被传递给表单的 DialogResult 并且表单关闭。在属性窗口中将 button3.DialogResult 设置为 None 并在代码中将其设置为 Yes 仅当您想关闭表单时。
【解决方案2】:

如果您的button3 按钮将其属性DialogResult 设置为与DialogResult.None 不同的值,则会发生此行为。 (查看属性窗口)

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

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

所以我建议将 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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多