Results 1 to 3 of 3

Thread: Change bgcolor of changed text box

  1. #1

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Exclamation 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
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Lightbulb 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
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width