Click here to Skip to main content
5,788,212 members and growing! (16,830 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate License: The Code Project Open License (CPOL)

Form Changed Control

By Duncan Edwards Jones

A component that allows you to monitor all the controls on the form and list any that have changed (for dirty checking)
C# (C# 1.0, C# 2.0, C#), VB (VB 7.x, VB 8.0, VB), .NET, WinForms, Dev

Posted: 28 Jun 2007
Updated: 26 May 2008
Views: 16,577
Bookmarked: 59 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 4.40 Rating: 4.62 out of 5
1 vote, 11.1%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 22.2%
4
6 votes, 66.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This control is a quick and dirty compontent (based on the System.ComponentModel.IExtenderProvider ) that allows you to list all the components that have been changed e.g. to allow you to decide whether a form needs to be saved.

Background

This is a quick and dirty solution to the "how do I know if any data on my form has changed" problem. It allows you to alter the user interface to show that it has changed and also to decide what needs to be saved.

Screenshot - changed_textbox.jpg

Using the code

The component has one property: ControlsThatHaveChanged that is a generic list of all the controls whose data have changed since the last reset. If this list isn't empty then the form has changed and needs to be saved.

There is one method: ResetDirtyFlags which allows you to reset the list of controls that have changed - call this when the form is populated or the data are saved.

In addition, an event is raised whenever a control that is being monitored for changes is changed or changed back to its initial state.

VB.Net
 
    Private Sub FormChangedComponent1_FormControlChanged(ByVal sender As Object, _ 
             ByVal e As FormChangedEventArgs) _ 
             Handles FormChangedComponent1.FormControlChanged
        If TypeOf e.ControlChanged Is TextBox Then
            If e.Changed Then
                e.ControlChanged.BackColor = Color.Yellow
            Else
                e.ControlChanged.BackColor = Color.White
            End If
        End If
    End Sub  

C#

        void formChangedComponent1_MonitoredControlChanged(object sender, FormChangedEventArgs e)
        {
            if (e.ControlChanged is TextBox)
            {
                if (e.Changed)
                {
                    e.ControlChanged.BackColor = Color.Yellow; 
                }
                else 
                {
                    e.ControlChanged.BackColor = Color.White;
                }
            }
        }

How it works

The component implements the Implements System.ComponentModel.IExtenderProvider to extend the controls on a form and provides thre new properties to each of these controls:

Screenshot - properties.jpg

MonitorForChanges - Set to True to monitor a control for changes

ChangeEventToMonitor - Set this to the name of the event to watch (e.g. "ValueChanged" or whichever for the control)

ValueNameToMonitor - Set this to the name of the property that represents the value of the control (e.g. "Text" for a textbox)

Note that these properties are case sensitive which can be a trap for the unwary

Where a component has this extended property set to True the component adds a handler to its "Changed" event specified and when that event fires, it updates an internal generic collection of the components that have changed

The component can be reset (i.e. all controls are marked as unchanged) after a save event or when a record has been refreshed

VB.net
    
Public Sub SetMonitorForChanges(ByVal ctl As Control, ByVal value As Boolean)
        If value Then
            If Not _ControlChanged.ContainsKey(ctl) Then
                _ControlChanged.Add(ctl, False)
                Dim evi As System.Reflection.EventInfo
                evi = ctl.GetType.GetEvent(Me.GetChangeEventName(ctl))
                If Not (evi Is Nothing) Then
                    '\\ Get the method that adds a handler to the changed event
                    Dim mi As System.Reflection.MethodInfo
                    mi = evi.GetAddMethod(False)
                    '\\ add a handler to that changed event
                    mi.Invoke(ctl, New Object() {Me.ChangeEventhandler})
                End If
            End If
        Else
            If _ControlChanged.ContainsKey(ctl) Then
                _ControlChanged.Remove(ctl)
            End If
        End If
    End Sub

C#

            private void ResetMonitoringState()
            {
                System.Reflection.EventInfo evi;
                Type ctlType = _ctlToMonitor.GetType();
                evi = ctlType.GetEvent(_ChangeEventName);

                if (_MonitoringEvent )
                {
                    // Remove the event handler from the control
                    if ( evi != null)
                    {
                        System.Reflection.MethodInfo mi = evi.GetRemoveMethod(false);
                        mi.Invoke(_ctlToMonitor, new object[] { this.ChangeEventhandler() });
                    }
                }
                
                if (_Monitor )
                {
                    // Add the event handler to the control
                    if (evi != null)
                    {
                        System.Reflection.MethodInfo mi = evi.GetAddMethod(false);
                        mi.Invoke(_ctlToMonitor, new object[] { this.ChangeEventhandler() });
                        _MonitoringEvent = true;
                    }
                }
            }

History

2007-06-28 First release

2007-07-03 Added properties to specify what event and value are being monitored and changed the code to do a true changed check using the hash value of the property

2007-09-13 Added an event whenever a control being monitored changes or is changed back to its previous state

2008-05-26 Added C# code version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Duncan Edwards Jones


Microsoft MVP 2006, 2007
Visual Basic .NET
Occupation: Software Developer (Senior)
Location: Ireland Ireland

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
GeneralGreat tool, how would I track original values?memberyarborg10:03 24 Jun '08  
GeneralRe: Great tool, how would I track original values?memberDuncan Edwards Jones10:32 24 Jun '08  
GeneralRe: Great tool, how would I track original values?memberyarborg10:42 24 Jun '08  
GeneralProblem with combo Box Changememberhardsoft17:57 26 May '08  
GeneralRe: Problem with combo Box ChangememberDuncan Edwards Jones23:48 26 May '08  
GeneralVB6 HELPmembermorrisons12:55 28 Apr '08  
GeneralRe: VB6 HELPmemberDuncan Edwards Jones21:03 28 Apr '08  
QuestionNice tool, but not all controls...membernecto0:48 3 Jul '07  
AnswerRe: Nice tool, but not all controls... [modified]memberDuncan Edwards Jones1:13 3 Jul '07  
GeneralVery Nice - How to change the default value?memberDean_DOT7:28 29 Jun '07  
GeneralRe: Very Nice - How to change the default value?memberDuncan Edwards Jones12:19 29 Jun '07  
GeneralRe: Very Nice - How to change the default value?memberDuncan Edwards Jones2:34 3 Jul '07  
GeneralGood workmemberjoebeam6:25 28 Jun '07  
GeneralRe: Good workmemberDuncan Edwards Jones6:44 28 Jun '07  
GeneralRe: Good workmemberDuncan Edwards Jones2:21 10 Jul '07  
GeneralRe: Good workmemberjoebeam9:43 10 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 May 2008
Editor:
Copyright 2007 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project