I have properites set up in my classes like this
private bool _IsDirty = false;
public int SomeProperty
{
get
{
return _SomeProperty
set
{
if(_SomeProperty != value)
{
_SomeProperty = value
_IsDirty = true // which mean the value has changed
}
}
}
now i have this in my form closing event because if the user changes information but tries to close the form before he clicks the save button it goes here....When the form loads it shows all the information pertaining to an object...the object properties are all binded to controls on the form so thats where the _IsDirty checks to see which ones have changed....
LoadFormToObject();// this is the binding of controls to the object properties
if(_Vendor.IsDirty)// Isdirty always evaluates to true even when nothing is changed{
DialogResult _result = MessageBox.Show("Would you like to save your changes to this
LoadFormToObject();
if(_Vendor.IsDirty){
DialogResult _result = MessageBox.Show("Would you like to save your changes to this Vendor ", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if(DialogResult.Yes == _result){
this._Vendor.Save();}
else if(DialogResult.Cancel == _result){
e.Cancel =
true;}
endor ", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);if(DialogResult.Yes == _result)
{
this._Vendor.Save();
}
else if(DialogResult.Cancel == _result)
{
e.Cancel = true;
}

Values dont change but.....
XJDM003
Samyag1
Here are the constructors....in the initial declaring of the varibles i do private bool _IsDirty = false;
public static Vendor GetNewVendor(){
Vendor _Vendor =
new Vendor();_Vendor._IsNew =
true; return _Vendor;}
public static Vendor GetExistingVendor(int VendorId){
Vendor _Vendor =
new Vendor();_Vendor._VendorID = VendorId;
_Vendor.Load();
return _Vendor;}
mrbelk
msedi
Kevin ORiordan
The sequence should be
-Load Object
-Load Object To Form
-Set dirty flag to false
-Allow users to interact with the form
-Load Form back into Object (setting dirty flag if values differ)
-Check dirty flag
-save if necessary
swapna_n
Yes! Problem is there you set isDirty value to false in Constructor or Load, Then prperty binding makes it to true becuase it changes control's value.
Try isDirty = false in FormActivated or FormShown event rather than constructor or Load!
I hope this will work!
Best Regards and Cheers ;-)
Ayukawa
Well basically I want to know how do i prompt the user that he changed some values on the form and tries to close the form before he saved that information...flow should be like
changed info on a form
tries to close the form before saving
message box( You changed something on the form you wanna SAVE )
click Yes
Save Method is called form closes
else
do something else.......
I have my form binded like textbox1.text = Object.Property....etc
I have a bool flag called _IsDirty
when i set my property i do
If(_Property != to the VALUE)
Set my property to the value and change IsDIrty = true.....I do this for every property in my class....
so when my form loads it has all the information for the object i have in focus.....now if i change the information it always goes to the messagebox saying _IsDirty is true when infact i just load the form and close it
this is the close event
LoadFormToObject();
if(_Vendor.IsDirty)---this changes to true even though i dont change anything on the form....{
DialogResult _result = MessageBox.Show("Would you like to save your changes to this vendor ", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if(DialogResult.Yes == _result){
this._Vendor.Save();}
else if(DialogResult.Cancel == _result){
e.Cancel =
true;}
}
}
Camenwolf
this is the LoadFormToObject...It seems that its set when the form loads but i do not have it in the constructor though....
private
void LoadFormToObject(){
try{
_Vendor.VendorStatus = cboVendorStatus.SelectedIndex;
_Vendor.Name = txtName.Text;
_Vendor.CreatedBy = lblCreatedByValue.Text;
DateTime FirstDate =
new DateTime(1753, 1,1); if(lblCreatedOnValue.Text == ""){
_Vendor.CreatedOn = FirstDate;
}
else{
_Vendor.CreatedOn = Convert.ToDateTime(lblCreatedOnValue.Text);
}
_Vendor.Address = txtAddress.Text;
_Vendor.Address2 = txtAddress2.Text;
_Vendor.State = txtState.Text;
_Vendor.City = txtCity.Text;
_Vendor.Country = txtCountry.Text;
_Vendor.Zip = txtZip.Text;
_Vendor.FederalID = txtFederalID.Text;
_Vendor.Phone = txtTelephone.Text;
_Vendor.Attention = txtAttention.Text;
_Vendor.Ext = txtExt.Text;
_Vendor.Activity = dtmActivity.Value;
_Vendor.ScacCode = txtScacCode.Text;
_Vendor.Fax = txtFax.Text;
_Vendor.EMailAddress = txtEmailAddress.Text;
_Vendor.WebURL = txtWebAddress.Text;
_Vendor.RecordStatus = cboRecordStatus.SelectedIndex;
_Vendor.DOTNo = txtDOTNumber.Text;
_Vendor.RSPANo = txtRSPANumber.Text;
_Vendor.DOTRating = cboDOTRating.Text;
_Vendor.RecordStatus = cboRecordStatus.SelectedIndex;
_Vendor.SelectionCode1 = txtSC1.Text;
_Vendor.SelectionCode2 = txtSC2.Text;
_Vendor.SelectionCode3 = txtSC3.Text;
_Vendor.SelectionCode4 = txtSC4.Text;
_Vendor.Notes = txtNotes.Text;
_Vendor.AddNotes = txtAdditionalNotes.Text;
_Vendor.IsContract = chkContract.Checked;
_Vendor.IsBroker = chkBroker.Checked;
_Vendor.IsForw = chkForwAuthority.Checked;
_Vendor.IsExempt = chkExempt.Checked;
_Vendor.ContractDate = dtmContract.Value;
_Vendor.IsContract = chkContract.Checked;
_Vendor.IsCommon = chkCommon.Checked;
_Vendor.CommonDate = dtmCommon.Value;
_Vendor.DateExempt = dtmExempt.Value;
_Vendor.BrokerDate = dtmBroker.Value;
_Vendor.ForwDate = dtmForwAuthority.Value;
}
catch(Exception ex){
MessageBox.Show(ex.StackTrace.ToString());
}
}
Tarek Ghazali
it may work but in the long term, no. - you need to make a robust/decent solution and make sure it goes a very long way than putting in small "patches" or workarounds
going back to the original question - would be great to see if you can debug where its setting it to true by stepping into the debugger on the form loading events as well as seeing if the code in the LoadFormToObject has something to do with it
Kesava
Dan Lingman
Juan Carlos Trimiño
I also had the same problem in future, Yes that's True what you said, Activated is not good place for that can a form can get activated more than one time. But FormShown us OK you can use that untill you show or hide form It'll work for you. by default it occurs for one time only unless you show or hide form again and again. but there is also work around for this too.
Soo I cant see any problem in using this in FormShown!
Best Regards,
andyhull
barkingdog
do _isDirty in FormShown Event handler and see what happens!
Best Regards,