|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
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
IntroductionThis 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. BackgroundThis 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.
Using the codeThe component has one property: There is one method: 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 worksThe component implements the
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;
}
}
}
History2007-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
|
||||||||||||||||||||||