Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 665393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:42:12+00:00 2026-05-13T23:42:12+00:00

I have a form that is bind to an object, and when the user

  • 0

I have a form that is bind to an object, and when the user trying to leave the form, I want to warn them if anything on the form has been changed, and prompt them to save. My question is, is there any way to achieve this without implementing IComparar for all my classes in the binded object? I was thinking if there is a way I can serialize my object when loading the form, and do a simple comparison against the change object that also get serialized. Something like a string comparison.

Hope that make sense,

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:42:12+00:00Added an answer on May 13, 2026 at 11:42 pm

    To do what you’re trying to do, there is probably a much simpler method: Use a “modified” flag or event.

    You can cascade this up many levels of user controls if need be. Just declare the event like this:

    public class MyControl : Control
    {
        public MyControl()
        {
            InitializeComponent();
            textBox1.TextChanged += BubbleModified;
            // etc.
        }
    
        protected void BubbleModified(object sender, EventArgs e)
        {
            OnModified(e);
        }
    
        protected void OnModified(EventArgs e)
        {
            var handler = Modified;
            if (handler != null)
                handler(this, e);
        }
    
        [Category("Behavior")]
        [Description("Occurs when data on the control is modified.")]
        public event EventHandler Modified;
    }
    

    Then, at whatever level you need to actually check for modifications, hook all of the events.

    public class MainForm : Form
    {
        private bool isDataModified;
    
        public MainForm()
        {
            InitializeComponent();
    
            textBox1.TextChanged += DataModified;
            textBox2.TextChanged += DataModified;
            // etc.
            userControl1.Modified += DataModified;
            userControl2.Modified += DataModified;
            // etc.
        }
    
        private void DataModified(object sender, EventArgs e)
        {
            isDataModified = true;
        }
    }
    

    Then all you have to do is check (and reset) the isDataModified flag accordingly.

    It’s really not a lot of work, certainly easier than ensuring that INotifyPropertyChanged is implemented for every object in the graph. Remember, this is a form, you don’t really care that the object changed, you care if the user made a change, and for that, you want to actually check for changes made through the controls.

    Yeah, it’s not perfect – you still run into the minor nuisance of reporting that data was changed even when the user changes something and then changes it back. But I don’t think I’ve ever actually heard a complaint about this, and using serialization as a comparison method just isn’t reliable. What you really need to do if you want to eliminate the redundant save confirmation is override the Equals method of every object in the graph and implement an actual value-equality operation. Or, if you don’t want to retain a copy of the old graph, use a checksum-generating function (collisions are possible but highly unlikely).

    But I would just stick with the flag. Don’t try to cheat your way out of writing equality checks. It’s actually sort of the same as trying to write an automatic deep-cloning method; you can try, but anything you come up with is going to be broken sometimes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.