Transparent control

hadinatayp

Well-known member
Joined
Feb 8, 2006
Messages
95
Programming Experience
Beginner
Does anybody know how to make control transparant like this in vb.net?

thx

transparant1.jpg
 
Yes, nearly all controls support transparent backcolor's and you can toggle the amount of translucency using the fromARGB method.


VB.NET:
Control.BackColor = Color.FromARGB(Amount of transparency, R,G,B)
 
well, i've been trying to make one like that form, so i predict the menu on the left would me a listbox i suppose? and on the right it must be a panel which will be it's visible method based on the selected index on the left.

nah, the problem is, after i include the listbox on the form and set the backcolor of it : ListBox1.BackColor = Color.FromArgb(10, 255, 255, 255) in the formload method, and i got en error message state the control doesn't support transparent color :((...

any clues how to make a looks form like that?
thx
 
No the panel on the left isn't a listbox as you've discovered. I'm willing to bet that is a custom written collection based control and the one on the right is most likely the same thing. It could be a panel, but because it doesn't look like it contains any controls i am almost certain it isn't. So to write on of these isn't going to be a snip, but the implementation is fairly simple so long as you know about writing custom collection based controls.

The Background of the actual form look like it's been painted with a PathGradientBrush with it's center point set to the bottom right corner and the interpolation colors set to the colors you can see.

Then you have the panel controls, which are most likely the same custom control just a different size and with different text. You'll need to start by creating a new control that inherits from the base class control, set it's style bits so that it supports a transparent backcolor

VB.NET:
Mybase.SetStyle(ControlStyles.SupportsTransparentBackColor, True)


Create a collection that inherits from collection base and the functions/subs to add remove the items that you will create. Which will be a class that inherits from component. Thats where you would start, once you have the collection up and running and the components post back and i'll help further. In the mean time i'll try to get you a working skeleton demo.
 
do you mean, i just create a class that inherits form a control?

like :

VB.NET:
   Public Class CustomListBox
    Inherits ListBox

    Public Sub New()        
        MyBase.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    End Sub

End Class
 
i just know that listbox control inherits form listControl, so i tried to make one that inherits from it, then the problem is i don't know what should i filled in the overrides method :(

VB.NET:
Public Class CustomListBox
    Inherits ListControl

    Protected Overrides Sub Refre****em(ByVal index As Integer)

    End Sub

    Public Overrides Property SelectedIndex() As Integer
        Get

        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Protected Overrides Sub SetItemsCore(ByVal items As System.Collections.IList)

    End Sub
End Class
my goal is to create a custom listbox that can support a transparency background like the one on the top.
does anyone know how to make one?

thx
 
Here is an article from codeproject that shows you the basic framework for a custom "listbox": WinForms Custom Container Control by Victor Boba
The example doesn't handle transparent backgrounds as is but it's very simple to modify it so that it does. In the ChildItem class's New procedure, add the SupportsTransparentBackColor ControlStyles flag so that it reads:
VB.NET:
Me.SetStyle(ControlStyles.DoubleBuffer _
  Or ControlStyles.UserPaint _
  Or ControlStyles.AllPaintingInWmPaint _
  Or ControlStyles.SupportsTransparentBackColor, _
  True)
Also change the BackColor of the CustomContainerControl class to Transparent. Now it supports transparency, just set the BackColor of the ChildItem class to some semi transparent color [i.e.: BackColor = Color.FromArgb(100, Color.White)].

As I said, the example is just a basic framework and should be extended. For example the BackColor (both selected and normal states) of the ChildItem could be made a property instead of hardcoded. It could also be simplified somewhat by using new features of the .NET framework V2; instead of using the strongly typed custom collection class named SimpleItemCollection, one could use a System.Collections.Generic.List(Of SimpleItem) instead.

I also quickly added Mouse Enter and Leave events in the ChildItem class to change the color when moused over:
VB.NET:
Private Sub ChildItem_MouseEnter(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Me.MouseEnter
    Me.BackColor = Color.FromArgb(150, Color.Yellow)
End Sub

Private Sub ChildItem_MouseLeave(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Me.MouseLeave
    'the selected property already sets the BackColor based
    ' on whether or not the item is selected so use it:
    Me.Selected = Me.Selected
End Sub
This also should be extended to support a property for the hover color.
 
Back
Top