tags:

views:

938

answers:

2

Hi

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,

A: 

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.

STW
Thanks Yoooder I have tried your solution but am afraid it did not work. I added the code to the constructor of the form after InitialiseComponent.
Prithis
+1  A: 

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.

Paul Alexander
I think Paul is correct. I tried monkeying around with the Paint events and could not get it to do anything useful to your situation. You might get some hack working where you only paint the non-intersecting rectangles or something, but that seems too complicated. It might just be easier to add as a child control, even if dynamically.
jomtois