My child control does not refresh

General Fear

Member
Joined
Dec 22, 2012
Messages
12
Programming Experience
10+
I am using VB.Net 2010.

So here is my problem. I created a custom control. I wanted this to be my base class so that all textboxes are the same. If I want to say change the background color I can change it in one place.

I created my base class. It had a yellow background. Then I created a Win Form app. I bring into the app the DLL that is my base class. I see it in the toobox. I drag and drop. Perfect the text box is yellow. I close the Win Form app. Then I go back to my base class. I change the background to white. I open the Win Form app and it is still YELLOW? When I delete the textbox and put a new textbox from my base class, it is white.

I hoped that if I changed the parent textbox class all the children will automatically get the new changes I made. What am I doing wrong?
 
Hi,

This depends on where you have created the code for changing the BackColor in the custom control. If you have done this coding in a Public Sub New() method then this would be the expected behaviour.

To accomplish what you are trying to do, try moving the code to the HandleCreated event of the control. i.e:-

VB.NET:
Public Class NewTextBox
  Inherits TextBox
 
  Private Sub NewTextBox1_HandleCreated(sender As Object, e As EventArgs) Handles MyBase.HandleCreated
    MyBase.BackColor = Color.Blue
  End Sub
End Class

Once you make a change to this event in the custom control all you have to do is rebuild the project where you are using the custom control and all the controls will change to reflect the changes.

Hope that helps.

Cheers,

Ian
 
Most likely you should be using/overriding a Property, set the default value, and apply DefaultValue attribute.
 
Back
Top