Reputation: 21
After retriving the data from database, When I click the Update button there will raise a messagebox as "Changes has done.Do u like to proceed" with Yes/No buttons. But i want to raise the message box when I made any changes only. If i didn't make any changes it should not raise.. Please help me how to know the changes in my textboxes and comboxes. My code for raising is
DialogResult result = MessageBox.Show("Chanes has done, Do you wish to save changes.?", "Message",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
// HERE MY UPDATE CODE.
}
Upvotes: 1
Views: 1898
Reputation: 24470
You'll need to store the original values as strings, then compare these to the resulting values.
Alternatively a simpler implementation could just set a boolean isDirty
flag to true on the text box changed event, and reset to false on a successful update.
Example code to help highlight what's needed included below:
if(ValuesHaveChanged())
{
DialogResult result = MessageBox.Show("Data has been changed, do you wish to save changes?", "Save Changes",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
// HERE MY UPDATE CODE.
ResetChangeDetection();
}
}
.
private bool ValuesHaveChanged()
{
return this.isDirty;
/*
return !
(
this.savedName.Equals(NameTextbox.Text)
&& this.savedAddress.Equals(AddressTextbox.Text)
)
*/
}
.
private void ResetChangeDetection()
{
this.isDirty = false;
//this.savedName = NameTextbox.Text;
//this.savedAddress = AddressTextbox.Text;
}
Upvotes: 0
Reputation: 1396
Option 1: Compare old values with new values before showing message.
Option 2: Add a member variable dirty to your form and register to change events of every sub control.
In event handlers: Check if value has been changed. If so set dirty to true.
Option 3: (My favorite.) Do not do any check because it is too hard to maintain these checks. You need to change your checks for every change in your data structure or sub controls.
Just remove "Changes has done" from your message. User knows himself whether he has really changed his data. So just ask "Do you wish to save changes?".
Upvotes: 2