0

I would like to draw a label on top of another label. The top label's backcolor is transparent. I would like to see the color of the bottom label's backcolor, not the backcolor of the form both labels are on. Currently when I set a label to transparent I can see through all controls behind the transparent label straight through to the form background.

I think what I need to do is override the OnPaint method and skip drawing anything that is transparent, as takrl shows with his answer to this question: How to create a transparent control which works when on top of other controls?

My problem is that Parent is always null. How do I find the parent control in the OnPaint method in VB.Net?

Protected Overrides Sub OnPaint(e As PaintEventArgs)

End Sub

EDIT 1: I cannot post pics because I do not have 10 reputation. I have a label that surrounds many other labels, lblBorder. lblBorder is invisible always except when the mouse enters it. It acts as a type of highlighter. When lblBorder, or any label inside lblBorder, is clicked a menu appears.

Community
  • 1
  • 1
Dennis
  • 51
  • 1
  • 8
  • If the Parent is null, then you can't see the control anyway. Check for null. – LarsTech Jul 10 '14 at 16:34
  • ...as in the first line of code linked to: `if (Parent != null)...` – Ňɏssa Pøngjǣrdenlarp Jul 10 '14 at 16:38
  • Transparency is simulated, it draws the Parent as the background. So if you don't want to see the form then the label's Parent has to be the bottom label. The Parent is not "always null", do not put code like that in OnPaint. Using TextRenderer.DrawText() in OnPaint() instead of Label controls is an entirely smart way to produce transparency effects, labels are very wasteful point-and-click resources. – Hans Passant Jul 10 '14 at 16:45
  • Please add a screenshot to explain what's happening and the behavior you are trying to achieve. – Victor Zakharov Jul 10 '14 at 17:22
  • Thank you for the responses. DrawText will fix part of my problem, but not the part stated in my edit. I have `if Parent <> null` in my code but the problem is that it is always null. I guess I don't really understand overiding the OnPaint routine because even when nothing is in the routine my controls are drawn anyways. Is this because I created the controls in design mode? Is that different than creating controls programmatically? – Dennis Jul 10 '14 at 18:03
  • Actually I don't think the DrawText will work for my application. The text that I wish to display is constantly updated. It is to be displayed on top of an image. – Dennis Jul 10 '14 at 18:17
  • To clarify my edit from above. lblBorder is a label with a black border and a transparent background. – Dennis Jul 10 '14 at 18:22
  • You can cause your label to redraw again when it needs to be updated by calling .Invalidate on it. It will then update the next time your application handles events. – Bradley Uffner Jul 10 '14 at 18:28

0 Answers0