Checking if child control has had its BackColor set by the user

303 views Asked by At

I'm having a lot of difficulties with a custom Panel I'm creating.

It has rounded corners and as such its BackColor is only visible in the corners and the main colour of the Panel is a separate color, m_mainPanelColour. Unfortunately, when I add a control, the control I'm adding sets its BackColor to the Panel.BackColor, not m_mainPanelColour.

I have overridden the OnControlAdded Event to set the BackColor of the newly added control to the m_mainPanelColour, however I've realised I only want this behaviour if the control's BackColor has not been explicitly set by the user.

My issue is, that I don't know how to query the control for this scenario. It must be possible however because the standard Windows Panel does it.

Does anybody have any suggestions? Thank you in advance.

1

There are 1 answers

2
Nathan M On

You might want to declare a Sub New() in your custom control and add the following lines of code, before InitializeComponent():

SetStyle(ControlStyles.ResizeRedraw Or _
         ControlStyles.AllPaintingInWmPaint Or _
         ControlStyles.OptimizedDoubleBuffer Or _
         ControlStyles.SupportsTransparentBackColor Or _
         ControlStyles.UserPaint, True)

UpdateStyles()

This will enable transparent background color support. This way, you can make the default color on the control Color.Transparent, and so it will be painted if, and only if, the user of the control selects a different color. Of course you will have to take over painting the entire control, yourself.

Usually e.Graphics.Clear(Color.Transparent) will work to quickly fill the control with transparent black (&H0I). But remember, if you access the Graphics object outside of a paint event (by calling Me.CreateGraphics, for example), you will not be able to easily clear the background to transparent.