• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

ExtenderProvider invalidated event problems...

VB.NET 2005

I have a component class that adds several provider extensions to a form and one of them is a Required property along with a Required color property and a Satisfied color property.  I add a xxxChanged event handler that checks to see if the control's text is empty and if it is, I set the backcolor of that field to the Required color and if it is not empty, then I set it to the Satisified color.  I was having trouble before with trying to get the control (textbox, combobox, etc.) painted initially until I started handling the Invalidated event.  This worked fine for the textbox, combobox but failed for the NumericUpDown control.  It fails with an infinite loop where the invalidated event triggers the text changed event triggering the invalidated event, ad nauseum.  So, then I thought that perhaps I might trap the Paint event of the container that contains the control I'm extending, but I've not been too successful with this approach.  Is there an event I can trap for a control when it is first displayed on a screen so that I can set its initial backcolor correctly depending on if it is empty or not?
  • 2
1 Solution
Generally, you would need to use the OnPaint event for the control itself, but it appears that Windows does the painting for things such as Textbox controls (even if you inherit from them)

See here for full details, and a possible workaround:

Just had another idea which could work, depending on how you're working the application:

For the control you want to change the backcolor of, create an function for the Validating event, such as the following:

    Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
       ' If text is empty, draw with a black background, otherwise set to white
        If String.IsNullOrEmpty(TextBox1.Text) Then
            TextBox1.BackColor = Color.Black
            TextBox1.BackColor = Color.White
        End If
    End Sub

Then, place the following line in your Form_Load method:


That way, if the textbox is empty when the form first loads, it will validate it straight away, and automatically set the background colour.

Hope that helps, though I'm not sure if it's exactly what you need.........

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now