1

I have a form that is linked to a database and allows a user to insert, update and delete records. I want the user to be notified of any unsaved changes when they click to go back to the main menu, and have the option whether or not to save them.

The code I have so far is below, but it doesn't recognize the changes. It just goes straight back to the main menu.

private void btnBack_Click(object sender, EventArgs e)
    {
        frmMenu frmMainMenu = new frmMenu();

        if (dsOrders.HasChanges())
        {


            if (DialogResult.Yes == MessageBox.Show("There are changes that have not been saved and will be lost. Would you like to save them before leaving this form?", "Unsaved Changes", MessageBoxButtons.YesNo))
            {
                dsOrders.AcceptChanges();
            }
            else
            {
                frmOrders.ActiveForm.Hide();

                frmMainMenu.Show();
            }

        }
        else
        {
            frmOrders.ActiveForm.Hide();

            frmMainMenu.Show();
        }

    }
tshepang
  • 12,111
  • 21
  • 91
  • 136
6TTW014
  • 627
  • 2
  • 11
  • 24

2 Answers2

2

I am guessing you are using DataBinding and dsOrders is a dataset.

What you could try to do is to check if your databinding is working correctly (both-ways) by setting a breakpoint just before the menu is called.

Then, you could edit some data, and when the breakpoint is triggered, check if the changes are in the dataset. If they are not, the HasChanges method will return false, and you won't get the messagebox.

An other way to check if your data has changed to show this message would be to attach an eventhandler on the change event of all the controls on your form. In this event handler you could set a boolean like blnChanged to true and check its value instead of dsOrders.HasChanges

Martin
  • 5,954
  • 5
  • 30
  • 46
2

One way to do is use the individual controls' Changed event and set a dirty bit

for eg

    public bool Dirty { get; set; }


    private void textBox1_TextChanged(object sender, EventArgs e)
    {
          Dirty = true;
    }

and then

        if (Dirty)
        {


            if (DialogResult.Yes == MessageBox.Show("There are changes that have not been saved and will be lost. Would you like to save them before leaving this form?", "Unsaved Changes", MessageBoxButtons.YesNo))
            {
                dsOrders.AcceptChanges();
            }
            else
            {
                frmOrders.ActiveForm.Hide();

                frmMainMenu.Show();
            }

        }
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • I've tried this but when I navigate through the dataset because the text in the texboxes is changing it assumes I have edited the record – 6TTW014 Mar 24 '11 at 23:52
  • When the boxes are filled by the dataset the changed events are triggerd. Make sure to set `Dirty = false` after loading the form finished and after every click on navigation button. – grimmig Apr 21 '11 at 08:02