Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不规则地关闭消息框窗体_C#_Winforms_Forms_Messagebox_Dialogresult - Fatal编程技术网

C# 不规则地关闭消息框窗体

C# 不规则地关闭消息框窗体,c#,winforms,forms,messagebox,dialogresult,C#,Winforms,Forms,Messagebox,Dialogresult,我使用一个消息框来确保用户是否想从gridview中删除一行,但是不管他们给出什么答案,它都会关闭表单并返回到form1 这是加载viewTransactions表单的地方。此代码位于表单1中 private void btnViewTrans_Click(object sender, EventArgs e) { viewTransactions = new View_Transactions(newList); if (viewTransactions.ShowDialo

我使用一个消息框来确保用户是否想从gridview中删除一行,但是不管他们给出什么答案,它都会关闭表单并返回到form1

这是加载viewTransactions表单的地方。此代码位于表单1中

 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
            }
        }
    }

在使用messagebox显示警告之前,我对代码没有任何问题。我相信DialogResult正在传递给另一个ShowDialog,这就是它关闭我的表单的原因。

如果您的
按钮3
按钮的属性
DialogResult
设置为与
DialogResult不同的属性,则会发生此行为。无
。(查看属性窗口)

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

通常,此错误是由于另一个按钮控件的
复制/粘贴
而导致的,该控件已通过属性设计器正确设置,以返回DialogResult。 顺便说一下,不需要初始化
新对话框result
的代码

因此,我建议将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;
    }
}

我通过添加this.dialogResult=dilaogResult.None解决了这个问题; 当按钮3_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的值。如果设置为除None之外的任何值,则您的表单将关闭。正如我所说,在放入messagebox之前,我没有遇到任何问题。删除元素并重新复制列表效果很好。我得到的最大错误是当窗体关闭时,它会将我带回到窗体1新列表。Equals不会运行,因为dialogResult是或否。Steve这就是发生的事情,但为什么?dialogResult不应该特定于调用的messagebox吗?我的messagebox是否应该有一个与viewTransaction表单的dialogResult不同的dialogResult?您能告诉我按钮3上dialogResult属性的值是多少吗?在设计器模式下,单击按钮,然后查看属性窗口。当按钮3\u单击被调用时,按钮3的对话框结果为cancel,并且始终为cancel,但是如果语句在属性窗口中运行良好,它会显示什么?属性窗口显示cancel现在请重新阅读我的答案。您已将按钮3设置为取消。该值被传递给窗体的对话框Result,窗体关闭。在属性窗口中将button3.DialogResult设置为None,并且仅当您要关闭表单时,才在代码中将其设置为Yes。