Change bgcolor of changed text box
Hi,
I have a data entry form with many text boxes/combo boxes/controls. I want to change their bgcolor everytime the user makes a change so they are aware of what changes they have made before commiting.
My plan is to store the original value in the tag and comapre in the validated event.
I don't won't to write the same code everytime I need a control to behave that way. I've never written a customcontrol before.
Can someone give me an example on how to achieve this by probably subclassing the control and reuse the code regardless of the type of control it is?
Thank you.
Sean
Re: Change bgcolor of changed text box
You don't actually need a custom control. Just write a single event handler and put every control in the Handles clause. You can then refer to the actual control that raised the event using the 'sender' parameter.
Re: Change bgcolor of changed text box
I wrote this and it works alright, Thanks for the quick reply anyway..
Code:
''' <summary>
''' Marks changed controls of type textbox, comobobox, checkbox and radio buton
''' </summary>
''' <remarks></remarks>
Public Class ControlChangeTracker
Private _con As Control
Private _cColor As Color = Color.FromArgb(255, 255, 98)
Private _dColor As Color
Sub New(ByVal con As Control)
_con = con
_dColor = _con.BackColor
If TypeOf _con Is CheckBox Then
Dim c As CheckBox = _con
c.Tag = c.Checked
AddHandler c.CheckedChanged, AddressOf Me.con_validating
ElseIf TypeOf _con Is RadioButton Then
Dim c As RadioButton = _con
c.Tag = c.Checked
AddHandler c.CheckedChanged, AddressOf Me.con_validating
Else
_con.Tag = _con.Text
AddHandler con.TextChanged, AddressOf Me.con_validating
End If
End Sub
Sub con_validating(ByVal sender As Object, ByVal ev As EventArgs)
Try
If TypeOf _con Is CheckBox Then
Dim c As CheckBox = _con
If Not c.Tag = c.Checked Then
_con.BackColor = _cColor
Else
_con.BackColor = _dColor
End If
ElseIf TypeOf _con Is RadioButton Then
Dim c As RadioButton = _con
If Not c.Tag = c.Checked Then
_con.BackColor = _cColor
Else
_con.BackColor = _dColor
End If
Else
If Not _con.Tag = _con.Text Then
_con.BackColor = _cColor
Else
_con.BackColor = _dColor
End If
End If
Catch ex As Exception
End Try
End Sub
To use try,
Code:
Private Sub frmViewAsset_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtAssetCode.Text = ast.AssetCode
chkBoxMeter.Checked = Not ast.isMetered
addTracker(txtAssetCode)
addTracker(chkBoxMeter)
End Sub
Private Sub addTracker(ByVal c As Control)
Dim x As New ControlChangeTracker(c)
End Sub