Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-05-06
4
Medium Priority
?
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Wikkard earned 450 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 30

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline

610 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