2

I use a message box to make sure if the user wants to delete a row from the gridview however no matter what answer they give it closes the form and returns to form1

This is where the viewTransactions form is loaded this code is in Form1

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

 }   

This is where the messageBox is shown in the viewTransaction form

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

I had no problems with the code until I used the messagebox to display a warning. I believe that the DialogResult is being passed to the other ShowDialog and that is why it is closing out my form.

Josh Davis
  • 90
  • 8
  • 2
    You might be running into problems deleting from a collection you're currently iterating through. That's usually bound to cause problems. – Mike Christensen Sep 26 '14 at 16:51
  • Look at the value of the property DialogResult for the button3 control. If it is set to anything but None your form will close. – Steve Sep 26 '14 at 16:54
  • like I said I had no problems before the putting in the messagebox. Deleting elements and recopying the list worked fine. The biggest error I get is when the forms close and it takes me back to form1 the newList.Equals doesn't run because the dialogResult is either yes or no. – Josh Davis Sep 26 '14 at 16:55
  • Steve that is whats happening, but why? shouldn't the dialogResult be specific to the messagebox called? Like shouldn't my messagebox have a dialogResult that is different from the dialogResult of the viewTransaction form – Josh Davis Sep 26 '14 at 16:56

2 Answers2

0

This behavior happens if your button3 button has its property DialogResult set to something different than DialogResult.None. (Look at the Property Window)

When you click a button the DialogResult property is passed to the DialogResult property of the form and if it is different from None, the form is closed.
This is how Modal forms communicate to the calling code the choices made by the user.

Normally this bug happens as a consequence of a Copy/Paste of another button control that is correctly set with the Property Designer to have a DialogResult returned. By the way, your code that initializes a new DialogResult is not needed.

So I suggest to set the button3.DialogResult property to DialogResult.None, and then, if the click on button3 results in a confirmation from the user set directly the Form.DialogResult property to 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;
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
0

I solved it by adding the this.dialogResult = dilaogResult.None; As soon as the button3_Click was called the base.DialogResult went to cancel for some reason

Steve it would still close when I tried your line, but thanks for telling me how to look that was how I figured it out

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

    }
Josh Davis
  • 90
  • 8
  • Could you tell me what is the value of the property DialogResult on the button3? In Designer mode click on the button and then look at the properties window. – Steve Sep 26 '14 at 17:11
  • When the button3_Click is called the DialogResult for button3 is cancel and is always cancel but the if statement works fine – Josh Davis Sep 26 '14 at 17:13
  • The Property Window please, what does it says? – Steve Sep 26 '14 at 17:14
  • property window says cancel – Josh Davis Sep 26 '14 at 17:17
  • Now reread my answer. You have the button3 set to Cancel. This value is passed to the DialogResult of the form and the form closes. Set the button3.DialogResult to None in the property window and set it to Yes in code only if you want to close the form. – Steve Sep 26 '14 at 17:18
  • i just tried that the form still closes. Like I said my answer works by adding in the this.DialogResult = DialogResult.None and everything else works fine. I also got rid of the dialogResult result – Josh Davis Sep 26 '14 at 17:20