2014-09-26 17 views
2

我用一个消息框,以确保如果用户想要删除GridView的一排但不管他们给什么答案吧关闭窗体,并返回到Form1消息框闭合形式erradically

这就是viewTransactions形式被加载这个代码是在Form1中,其中在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 
      } 
     } 
    } 

我与没有问题

private void btnViewTrans_Click(object sender, EventArgs e) 
{ 
    viewTransactions = new View_Transactions(newList); 
    if (viewTransactions.ShowDialog() == DialogResult.OK) 
    { 
     newList.Equals(viewTransactions.getList()); 
    } 

} 

这是直到我用消息框显示警告。我相信DialogResult被传递给另一个ShowDialog,这就是为什么它会关闭我的表单。

+2

您可能会遇到从当前正在迭代的集合中删除的问题。这通常会导致问题。 – 2014-09-26 16:51:12

+0

查看button3控件的属性DialogResult的值。如果它设置为除None之外的任何内容,则表单将关闭。 – Steve 2014-09-26 16:54:55

+0

就像我说的,在放入消息箱之前我没有任何问题。删除元素并重新列表清单工作正常。我得到的最大错误是当表单关闭时,它将我带回到form1,newList.Equals不运行,因为dialogResult是yes或no。 – 2014-09-26 16:55:37

回答

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

    } 
+0

你能告诉我button3上的属性DialogResult的值是什么?在Designer模式下,点击按钮,然后查看属性窗口。 – Steve 2014-09-26 17:11:47

+0

当button3_Click被调用时,button3的DialogResult被取消,并且总是取消,但if语句正常工作 – 2014-09-26 17:13:54

+0

请问属性窗口是什么? – Steve 2014-09-26 17:14:25

0

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

当您单击按钮时,DialogResult属性将传递给窗体的DialogResult属性,如果它与None不同,窗体将关闭。
这是Modal表单如何与调用代码进行通信的用户所做的选择。

通常这错误发生,因为这是正确与物业Designer中设置有一个的DialogResult返回另一个按钮控制的Copy/Paste的结果。 顺便说一句,您不需要初始化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; 
    } 
}