Save verification

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello,
I am developing an application which gui is very rich with controls. My
question is what is the best way to implement on c# a save require
mechanism? I mean to check before closing the application that the
changes in data in every control been saved?

Thanks!
 
Hi,

use a global boolean variable and set it to
true on every change of your data. right before
your application closes, check whether the varibale
is set to true and if yes, save the changes or ask
the user whether he/she/it wants the changes saved
If the user saves the data manually, just set the variable
to false on a "successfull" saving operation!


Regards

Kerem

--
 
Is there a more efficent way than just set the variable on every
control? I have a lot of controls and it seems that I need on update of
each one to update this global variable? No other more efficient way?

Thanks!
 
It is only one statement in the validating event setting a bool value to
true. I don't believe this to be inefficient.
 
But I need to update it each time someone updates any control? By the
way,how can I do it,is there an update event common to each gui control
in c#? Isn't therea more efficent way for this ?
 
But I need to update it each time someone updates any control? By the
way,how can I do it,is there an update event common to each gui control
in c#?  Isn't therea more efficent way for this ?

*** Sent via Developersdexhttp://www.developersdex.com***

In it's simpliest implementation this form cannot be closed if the
value in textBox1 changes and if the user hasn't saved (button1)

public partial class Form1 : Form
{
private bool _isDirty;
public Form1()
{
InitializeComponent();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs
e)
{
// check if data is dirty
if (_isDirty == true)
{
MessageBox.Show("You have a pending save");
// cancel the closing event
e.Cancel = true;
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
// If change occurs the is dirty is true
_isDirty = true;
}

private void button1_Click(object sender, EventArgs e)
{
// Save here

// if successful set isDirty to false
_isDirty = false;
}
}
 
In it's simpliest implementation this form cannot be closed if the
value in textBox1 changes and if the user hasn't saved (button1)

public partial class Form1 : Form
{
   private bool _isDirty;
   public Form1()
   {
      InitializeComponent();
   }

   private void Form1_FormClosing(object sender, FormClosingEventArgs
e)
   {
      // check if data is dirty
      if (_isDirty == true)
      {
         MessageBox.Show("You have a pending save");
         // cancel the closing event
         e.Cancel = true;
      }
   }

   private void textBox1_TextChanged(object sender, EventArgs e)
   {
      // If change occurs the is dirty is true
      _isDirty = true;
   }

   private void button1_Click(object sender, EventArgs e)
   {
      // Save here

      // if successful set isDirty to false
      _isDirty = false;
   }



}- Hide quoted text -

- Show quoted text -

Furthermore, you can reuse the textBox1_TextChanged handler by
attaching it to each control - you don't need a handler for each.
 
But if I don't want to keep the boolean inside the gui form,isn't there
any better design consept for holding and updating this info outside of
the form?

Thank you!
 
But if I don't want to keep the boolean inside the gui form,isn't there
any better design consept for holding and updating this info outside of
the form?

Thank you!

*** Sent via Developersdexhttp://www.developersdex.com***


Absolutely.... My previous was example was in it's simpliest form.
When Refactored.....

/* When DataItem is new, it is IsDirty.
When DataItem is loaded, it is not Dirty.
When DataItem property has changed, it is Dirty.
When DataItem is saved, it is no longer dirty.*/

public class MyDataItem
{
private bool _isDirty;
public MyDataItem()
{
_isDirty = true;
}

private int _id;
public int Id
{
get{return _id;}
}
private string _name;
public string Name
{
get{return _name;}
set
{
_name = value;
OnPropertyChanged();
}
}

public void LoadData()
{
_name = DALC.GetDataItem(...);
_isDirty = false;
}

public void Save()
{
DALC.Save(out _id);
// if save successful
_isDirty = false;
}
private void OnPropertyChanged()
{
_isDirty = false;
}
}

public partial class Form1 : Form
{
private MyDataItem _myDataItem;
public Form1()
{
InitializeComponent();
}

private void CreateNewItem
{
_myDataItem = new MyDataItem();
}

private void LoadData()
{
}

private void Form1_FormClosing(object sender, FormClosingEventArgs
e)
{
// check if data is dirty
if (_myDataItem.IsDirty == true)
{
MessageBox.Show("You have a pending save");
// cancel the closing event
e.Cancel = true;
}
}

private void button1_Click(object sender, EventArgs e)
{
// Save here
_myDataItem.Save();
}
 
Back
Top