Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends.
I have a form with some labels and a picturebox.
I want to make all the labels transparent. I have used the following code to do that. But, It makes some of the labels transparent and skips the others.

XML
Dim lbls As Control
      For Each lbls In Me.Controls
          If TypeOf lbls Is Label Then
              lbls.Parent = PictureBox1
              lbls.BackColor = Color.Transparent
          End If
      Next



So, Plz Help me that How can I make all the labels transparent on my Form.
Posted

Here you go:
Video Tutorial: Make a label transparent[^]


BTW, based on the issue you are facing might be because of label co-ordinates. Have a look at this thread discussion [^]that sounds similar.
 
Share this answer
 
Comments
rashidfarooq 12-Sep-10 4:56am    
Brother. My problem is not on making a label transparent. But my problem is that the above code is not applying on all the labels. So, plz be focused on it and tell me some solution. Thanks
Sandeep Mewara 12-Sep-10 14:34pm    
Yes bro! Did you saw the thread discussion I shared? It said a similar issue where labels cordinates were such that though the transparency was applied it was not visible.

Thing is we don't know your code and we are just trying to help you and suggest what could be possible reasons and the way to resolve. Hope you understand that.
If your form contains a container control like a panel or groupbox, your code will miss those. You'll need to check any panel or groupbox and loop through the controls they contain searching for labels as well.
 
Share this answer
 
Comments
rashidfarooq 10-Sep-10 9:09am    
Dear Brother.
There is no any panel or groupbox in my form. All the labels are on the form directly. So, can u guess what is the problem.
Kschuler 10-Sep-10 10:17am    
There must be something different about the labels that are not getting set to transparent. Perhaps they have visibility set to false in design time?
rashidfarooq 12-Sep-10 4:55am    
Brother! There is no such kind of problem. I have debugged this code and I have examined that the cursor exits before executing these labels. I mean that there are 9 labels and 1 picturebox on my form. So, in this for each loop when the iteration of the loop reaches to the picturebox at the 6th iteration, It exits and leaves the rest of the labels. While this loop should iterate 9 times, because there are 9 labels on the form.
Kschuler 13-Sep-10 8:40am    
You should add more information to the question so we can try to help. What version of Visual Studio are you using? What event handler calls the code that you have posted?
The above code should work. When are you running the code? In Form_Load?

That would make all the labels on the form transparent but if there are other labels in e.g. panels, they would not be affected because they are not in Me.Controls (the forms control collection).

Make sure that all the labels are on the form...

Another solution is to make the function recursive:

VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MakeLabelsTransparent()
    End Sub

    Private Sub MakeLabelsTransparent()
        LoopThroughCollection(Me)
    End Sub

    Private Sub LoopThroughCollection(ByVal parent As Control)
        Dim lbls As Control

        For Each lbls In parent.Controls
            If TypeOf lbls Is Label Then
                lbls.Parent = PictureBox1
                lbls.BackColor = Color.Transparent
            Else
                LoopThroughCollection(lbls)
            End If
        Next lbls
    End Sub


Please note: If you set a labels parent to PictureBox1 and the label is positioned completely or partially outside the picturebox, it will make a part or all of the label not show up at all...

Good luck
 
Share this answer
 

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