Solved

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

Posted on 2009-05-06
4
360 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 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 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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