Prithis
Prithis

Reputation: 1033

Transparent Panel on top of another normal panel

I want to show a transparent panel on top of another panel, both the panels have child controls like labels, text boxes etc. Transparency works fine if the transparent panel is child control of the other panel but if not then the label and text box of the normal panel appears on top of the transparent panel. Transparency for rest of the area works fine.

Any ideas ???

I have tried bring the transparent panel to the front but did not help. Perhaps I need to specify the order in which the controls should be drawn ?? If yes how do I do that ?

Interestingly if I move the application below the task bar and bring it up. It achives the right result. ( Reprinting resolves the issue !! but why??). However when I minimize it and restore it does not fix it!

Thanks,

Upvotes: 2

Views: 11945

Answers (3)

Cameron Stone
Cameron Stone

Reputation: 927

If you want a transparent control that won't obscure its siblings, according to Bob Powell you can do it by overriding the CreateParams method to add true transparency. Read the link for a full description.

Upvotes: 3

Paul Alexander
Paul Alexander

Reputation: 32367

Transparency in Windows.Forms is implemented by relational hierarchy rather than visual hierarchy. When a transparent control is painted, .NET basically calls up the Parent tree asking each parent control to paint itself, then paints the actual control content itself.

Two siblings in the same control will paint over each other.

So to answer the question, the topmost panel/control needs to be a child of the control you want to paint on top of.

Upvotes: 5

STW
STW

Reputation: 46366

If all your panels/labels/controls are part of a single UserControl then you are probably right that you just need to set the Z-order (the front-to-back order). You can do this using the Document Outline inside of Visual Studio's Designer under View > Other Windows > Document Outline.

If you're doing it programatically then you can play around with calling Control.BringToFront() or Control.SendToBack() to change their z-order. One possible way to do this is like (assuming ctl1 is intended to be at the back and ctl4 is intended to be front-mode.

SuspendLayout()
ctl1.BringToFront()
ctl2.BringToFront() ' Bring ctl2 in front of ctl1
ctl3.BringToFront() ' 3 in front of 2 (and in turn 1)
ctl4.BringToFront() ' 4 in front of the rest
ResumeLayout()

The Suspend/Resume Layout calls prevent the UI from updating while you rearrange things.

Upvotes: 0

Related Questions