Solved

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

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Selenium: Transitioning from unit testing to automated testing 4 62
TFS 2015 Access denied. 1 30
Return array 3 20
PowerShell: ForEach-Object Export to CSV 4 46
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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

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