Values dont change but.....

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;

}

 




Answer this question

Values dont change but.....

  • XJDM003

    Yea i have a LoadObjectToForm....here it is...do you think i should call both methods before i check to see if _IsDirty is true...I actually did that and i still get the same results.....

  • 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

    You talk a lot about the LoadFormToObject() method, but, somewhere you're going to need a LoadObjectToForm() method. It is immediately after the call to this method (actually, as the last step in the method would be best), you want to set IsDirty to false.

  • msedi

    sorry i don't follow..whats the question

  • 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

    no don't set it on FormActivated or FormShown event....thats not good practice. it would mean that even if there are actual changes to be made, the property will be true but if the form has no focus then its refocused or reactivated, then the property will be false...which is incorrect since there are changes to be made

  • Dan Lingman

    perhaps when you are assigning all the properties to the vendor object properties, somewhere in there its setting the IsDirty to true Again best thing to do is step in the debugger line by line and see whats going on and catch the part where it sets it to true. You could also just place a MessageBox for this purpose in the isdirty set property and see when it calls it...this would give you an indication on whats happening/where its calling from and then create a valid and proper solution

  • Juan Carlos Trimiño

    ahmedilyas wrote:
    no don't set it on FormActivated or FormShown event....thats not good practice. it would mean that even if there are actual changes to be made, the property will be true but if the form has no focus then its refocused or reactivated, then the property will be false...which is incorrect since there are changes to be made

    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

    step through the debugger see where its going wrong when you are loading the form - check the IsDirty property when stepping through the debugger and see at what point its setting it to true. Could it be that the LoadFormToObject() is doing it

  • barkingdog

    Tryin2Bgood wrote:

    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;

    }

    do _isDirty in FormShown Event handler and see what happens!

    Best Regards,



  • Values dont change but.....