Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form closing save changes before close

Tags:

c#

I have a Windows forms application in C# and I have a form that when the user closes it I ask, "do you want to save the changes"? How can I get the changes in my form? Here is some code:

public partial class DepartEdit : Form
{   
    string _nameDep; //This variavel get value textbox when form load

    {
        InitializeComponent();
    }
    private void DepartamentEdit_FormClosing(object sender, FormClosingEventArgs e)
    {


        if (txtNameDepart.Text != _nameDep && codDepartament > 0)//Here i compare
        {
            DialogResult dlg = MessageBox.Show("Save changes?", "Question", MessageBoxButtons.YesNo);

            if (dlg == DialogResult.Yes)
            {
                saveDepart(); // Metod save depart

                e.Cancel = false;

            }
            if(dlg ==DialogResult.No)
            {
                e.Cancel = false;

            }

        }        
}

There are a lot of textboxs and combo boxes? Is there any other way to get the changes in the form?

like image 440
user95542 Avatar asked Jul 13 '09 18:07

user95542


2 Answers

A lot will depending on where the information is held.

It you are using DataBinding you should be just monitoring the listChanged event or calling dataTable.GetChanges() if you are using a DataTable.

If the information comes from a class the implements ICloneable and IComparable, then you can take just take a backup copy when intialising the form (and after saving) and when closing you prepare you class for saving and compare it with the original.

Otherwise you can do something like

Declare a private variable

 private bool requiresSaving =false;

Declare an event

 private void SomethingChanged(object sender, EventArgs e)
 {
      requiresSaving  = true;
 }

Hook up this event to the various changed events, eg

 this.txtNameDepart.TextChanged += new System.EventHandler(this.SomethingChanged);

(The actual event is sometimes called something different , eg ValueChanged, SelectedIndexChanged , but they can all point to SomethingChanged unless you need a particular event to do something else.)

Check this variable when you are closing the form

private void DepartamentEdit_FormClosing(object sender, FormClosingEventArgs e)
{
    if (requiresSaving)
    {
      ....

You also need to set requiresSaving false in the saveDepart method.

Alternatively I have seem code where, when the control is being intialised, the tag value is also set, and the formclosing event loops through each control and compares the original values (in the tag object) with the current values.

like image 60
sgmoore Avatar answered Oct 05 '22 22:10

sgmoore


Create a string (or string[] I guess) within the Form_Load event and initialise them with the values present when the form first opens. eg

string oName = nameTextBox.Text;
string oCompany = companyComboBox.Text;

Then during the Form_Closing() event you can check these against the current values

private void Contact_FormClosing(object sender, FormClosingEventArgs e)
{
     if (oName!=nameTextBox.Text||oCompany!=companyComboBox.Text)
     {
         DialogResult result = MessageBox.Show("Would you like to save your changes",
             "Save?",
             MessageBoxButtons.YesNoCancel,
             MessageBoxIcon.Stop);
         if (result == DialogResult.Yes)
         {
             SaveFormValues();
         }
         else if (result == DialogResult.Cancel)
         {
             // Stop the closing and return to the form
             e.Cancel = true;
         }
         else
         {
             this.Close();
         }
     }
}
like image 25
Slotty Avatar answered Oct 05 '22 22:10

Slotty