Change textbox backcolor after data change?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dean Slindee

    Change textbox backcolor after data change?

    Anyone got a quick and easy way to change the background color on a textbox
    after the user has change the text value. Already have lots of forms
    written, so an approach that does not depend on adding code to an event on
    every textbox would be great.

    Just the same, I don't see an event specifically tailored for this task
    (like an OnChange event). Am I missing an obvious choice?

    Thanks,
    Dean S

  • Kerem Gümrükcü

    #2
    Re: Change textbox backcolor after data change?

    Hi Dean,

    once i wrote something like that, but this code is
    C#, you can easily convert it to VB.NET. Sorry
    Dude, i dont have VB.NET here yet, just C# and
    VC.NET,...
    Usage: You call "SetTextChangeH andlers()" at the end of
    your class constructor and it will assignt to every textbox it
    can find in the form the event handler so you dont have to
    set code for any textchange event again and again. It makes
    the background of a textbox red when it has no text and white
    when you fill some text into it,...

    //------------------CODE------------------

    private void SetTextChangeHa ndlers() {
    foreach (Control c in this.Controls)
    {
    try
    {
    if (c.GetType() == typeof(TextBox) )
    {
    c.TextChanged += new EventHandler(c_ TextChanged);
    }
    }
    catch (Exception)
    {
    }
    }
    }
    void c_TextChanged(o bject sender, EventArgs e)
    {
    TextBox t = ((TextBox)sende r);
    if (t.Text.Length == 0)
    {
    t.BackColor = Color.Red;
    }
    else
    {
    t.BackColor = Color.White;
    }
    }

    //------------------CODE END------------------

    This solution is fast, save and does not need
    a lot of code,...

    Regards

    Kerem


    --
    -----------------------
    Beste Grüsse / Best regards / Votre bien devoue
    Kerem Gümrükcü
    Microsoft Live Space: http://kerem-g.spaces.live.com/
    Latest Open-Source Projects: http://entwicklung.junetz.de
    -----------------------
    "This reply is provided as is, without warranty express or implied."


    Comment

    • Phill W.

      #3
      Re: Change textbox backcolor after data change?

      Dean Slindee wrote:
      Anyone got a quick and easy way to change the background color on a
      textbox after the user has change the text value. Already have lots of
      forms written, so an approach that does not depend on adding code to an
      event on every textbox would be great.
      Create you own Control, derived from TextBox, and add your logic to it.

      Class ChangeAwareTB
      Inherits TextBox

      Public Sub New()
      MyBase.New()

      Me.BackColor = Me.DefaultBackC olor

      End Sub

      Protected Overrides Sub OnTextChanged( _
      ByVal sender as Object _
      , ByVal e as EventArgs _
      )

      Me.BackColor = Me.ChangedBackC olour

      End Sub

      Public Shadows Property Text() as String
      Get
      Return MyBase.Text
      End Get
      Set( Value as String )
      MyBase.Text = Value
      Me.BackColor = Me.DefaultBackC olour
      End Set
      End Property

      Private ReadOnly Property DefaultBackColo ur() as Color
      Get
      Return SystemColors.Wi ndow
      End Get
      End Property

      Private ReadOnly Property ChangedBackColo ur() as Color
      Get
      Return Color.Azure
      End Get
      End Property

      End Class

      OK, you still have to revisit your existing code but only to replace the
      existing TextBox controls with your new ones, something like replacing...

      Private X As TextBox
      . . .
      X = New TextBox

      .... with ...

      Private X As ChangeAwareTB
      . . .
      X = New ChangeAwareTB

      HTH,
      Phill W.

      Comment

      • Kerem Gümrükcü

        #4
        Re: Change textbox backcolor after data change?

        Hi Phil,

        this would be the best solution, if he likes to
        extend the textbox,...

        Regards

        Kerem

        --
        -----------------------
        Beste Grüsse / Best regards / Votre bien devoue
        Kerem Gümrükcü
        Microsoft Live Space: http://kerem-g.spaces.live.com/
        Latest Open-Source Projects: http://entwicklung.junetz.de
        -----------------------
        "This reply is provided as is, without warranty express or implied."


        Comment

        • Duncan.Jones@bigfoot.com

          #5
          Re: Change textbox backcolor after data change?

          On Dec 13, 6:40 pm, "Kerem Gümrükcü" <kareem...@hotm ail.comwrote:
          Hi Phil,
          >
          this would be the best solution, if he likes to
          extend the textbox,...
          >
          Regards
          >
          Kerem
          >
          --
          -----------------------
          Beste Grüsse / Best regards / Votre bien devoue
          Kerem Gümrükcü
          Microsoft Live Space:http://kerem-g.spaces.live.com/
          Latest Open-Source Projects:http://entwicklung.junetz.de
          -----------------------
          "This reply is provided as is, without warranty express or implied."
          There is a component on the code project :

          that raises an event whenever any control on a form is changed.
          The demo app for the component changes the background colour for
          textboxes that have changed.


          Comment

          Working...