Solved

Visual C#, Is there any way to know if values are changed on form or not

Posted on 2009-05-06
4
352 Views
Last Modified: 2013-12-17
HI Friends,

I am working on VS 2008 with C# for a Windows Application development, I want to ask you that, is there any way to know if values are changed in any control on the form or not. I am using unbound controls in my windows form which fetch values from dataset on form load, I need to track if user make any change on the form or not, if yes then show  a message box alerting user data is changed since its loaded, i have around 45-50 controls on my form i.e textbox, combobox, radio buttons and buttons.

thanks,
Arun Vashist
0
Comment
Question by:ArunVashist
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Wikkard earned 150 total points
ID: 24321855
My suggestion would be to either to create new controls (inherited from the base ones that come with .net) that have an isDirty property or something that will be set whenever the values change. This way all the controls have the same property and you can simple check it.

The other (not very nice) option would be to create an event handler for each control onChange (the actual event name will differ depending on the control ie. checkboxes would have a checkedChange event.  This way you have 1 method which will be called for all the controls as they change. You will need to somehow keep track of which controls have changed.

public Form1()
        {
            InitializeComponent();
            this.textBox1.TextChanged += new EventHandler(CheckDirty);
            this.checkBox1.CheckedChanged += new EventHandler(CheckDirty);
            this.comboBox1.SelectedIndexChanged += new EventHandler(CheckDirty);

        }

        private void CheckDirty(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("Something changed: {0}",sender.ToString()));
   
        }


0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 24322257
I agree to Wikkard. The first way is the way the problem solved in the application I currently support.
But it's a harder way if you haven't ever created custom controls (not a big trick really).

The second way is also possible. really it's OK if you just need to know if something changed - just set some global isDirty = true. But if you need to keep track of what controls changed - you need to set some dictionary or any other collection for controls that were changed. And don't forget to clear after you don't need it. (I wouldn't go this way).
0
 

Author Comment

by:ArunVashist
ID: 24354745
Hi Friends,

Well the given solutions require lots of coding level efforts, actually i was asking if there is any way we could capture the value changed in textboxes, checkboxes or dropdowns using existing control properties/event.

Well suggested onChange event is there but again it require me to code individual control, so its not feasible for me. if anything else is available kindly suggest.

and Wikkard and Anarki, if possible please send me a sample code to create custom controls as suggested.

thanks,
0
 

Author Closing Comment

by:ArunVashist
ID: 31578820
Thanks, I am now using a Global bool variable which is set to true whenever there is any change in control value and using this value I raise a Alertbox. Thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now