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();
}