Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
2.78/5 (3 votes)
See more:
Hi guys.
I have two objects in my form (Visual Studio)
Panel1
ListView

The Panel1 is over the ListView.
But when I make the background of Panel1 to Transparency, it just show me the background picture of form.
I need to see my ListView in this area, not background of form.

Any help please???
Posted
Comments
gggustafson 8-Dec-13 17:27pm    
Why is Panel1 there at all? If there is a need, what is your order of drawing? Last one is on top.
BillWoodruff 8-Dec-13 23:41pm    
As Sergey points out: in practice simulating transparency in WinForms is just a mess, and, in this case, it's probably your design that needs changing. You want to see the ListView: bring it to the front, or hide the Panel.

Transparent doesn't work the way you think it does in WinForms. You described exactly the behavior that is supposed to happen. The reason is because when a control is put on the screen it had sole ownership of the pixels it occupies on screen. Only one control paints itself into those pixels. Transparent tells a control to take on the background properties of its parent container, the form in your case.

A better solution is to switch to WPF where this limitation doesn't exist. In WinForms you have to do some custom owner drawing of controls yourself.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 8-Dec-13 18:24pm    
Good advice, a 5.
I would even advise to never try to use control transparency in System.Windows.Forms and use WPF if this is really needed. However, one can create one's own semi-transparent object directly rendering them in some control using color alpha channel. This is a very different thing.
—SA
Sh.H. 9-Dec-13 10:00am    
Hi and thanks for comments.
Look, right now, I can't switch to WPF and have to continue in WinForm.

I just need to have an object, over my list view.

Now please let me know, how can I do that?
Is it possible to make a new object?
Or is it possible to use FlowPanel or PictureBox or Label or what ever , except panel?
Ot is it possible to make my object in WPF and bring it as refrence to here?

Please let me know any solution... what ever ... :(
Sergey Alexandrovich Kryukov 9-Dec-13 10:28am    
All of that is pretty much useless, including using of WPF in your form.
The last option will allow you to have independent interop control on your form where you can put some WPF controls. It is quite complicated solution to be best avoided, but won't solve your particular problem.
—SA
WuRunZhe 9-Dec-13 1:35am    
Valuable answer.
see: WinForms: How to create a control transparent to other controls

Edit: When I originally posted, I had just glanced at that code that accompanies the MS article. After looking at it, I realized that it has some implementation errors (go figure for a MS example). However, even if the coding errors are fixed, the technique used is in error. It does not paint the non-client area of obscured controls.

Here is a simplistic transparent panel control. Note that while in the designer, the panel will not be transparent. The reason for this is that because of the method used, it is difficult to edit the panel or its contents if it transparent. It may be possible to write a control designer to allow for easy editing while allowing it to be transparent, but I do not have the time right now to investigate this and I also dislike writing designers. :)

This control obtains the transparent effect by limiting the allowed region into which the control can paint. If you don't paint over it, you can see it.

Public Class TransparentPanel
   Inherits Panel

   Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
      SetPaintableRegion()
      MyBase.OnPaint(e)
   End Sub

   Private Sub SetPaintableRegion()
      ' intially set the paintable region as the entire control area
      Dim PaintableRegion As Region = New Region(New Rectangle(0, 0, Me.Width, Me.Height))

      ' if in design, do not do transparent
      If Not Me.DesignMode Then
         ' build a rectangle that represents the client area
         Dim clientrect As Rectangle = Me.ClientRectangle

         ' need to set the location of clientrect relative to Control.Location
         Dim offset As Point = ClientOriginRelativeToLocation(Me)
         clientrect.X = offset.X
         clientrect.Y = offset.Y

         ' subtract clientrect from PaintableRegion
         PaintableRegion.Xor(clientrect)

         ' now need to add in areas for child controls
         For Each c As Control In Me.Controls
            Dim r As Rectangle = c.Bounds
            r.X += offset.X
            r.Y += offset.Y
            PaintableRegion.Union(r)
         Next
      End If 'Not Me.DesignMode

      If Me.Region IsNot Nothing Then
         Me.Region.Dispose()
      Else
         Me.Region = PaintableRegion
      End If

   End Sub 'SetPaintableRegion

   Public Shared Function ClientOriginRelativeToLocation(ByVal c As Control) As Point
      Dim clientorigin As Point = c.PointToScreen(Point.Empty)
      Dim parentorigin As Point = Point.Empty
      If c.Parent IsNot Nothing Then
         parentorigin = c.Parent.PointToScreen(c.Location)
      End If
      Return New Point((clientorigin.X - parentorigin.X), (clientorigin.Y - parentorigin.Y))
   End Function 'ClientOriginRelativeToLocation

End Class ' TransparentPanel
 
Share this answer
 
v2
Comments
BillWoodruff 8-Dec-13 23:38pm    
A reasonable answer, given the question, but, in practice, this turns out to be a bloody mess.
Sh.H. 9-Dec-13 9:59am    
I tested that code.
It draws everything UNDER my objects.
Also it seems it is not possible to draw a ListView via that code you linked me.
TnTinMn 9-Dec-13 16:05pm    
I did not try the article code before posting link. My primary goal at the time was to point you to some documentation on the issue.

I have have revised my post and have included a simple transparent panel example. Just note that it will not appear transparent in the designer, but will appear transparent at runtime.

Edit: Just add the code to your project and build it. The TransparentPanel control should show up at the top of your toolbox. Then you can add it to your form.
TnTinMn 9-Dec-13 16:12pm    
If you are referring to the code in that article, you are of course correct. For a container control (like the OP's panel), a transparent effect can be easily created by setting the Region property. I have added an example of this to my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900