?
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
?
362 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

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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