Hey

I currently have

Private Sub LabelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label14.Click, Label15.Click, Label16.Click, Label17.Click

        If sender.backcolor = Color.LightGray Then
            sender.backcolor = Color.Green
        ElseIf sender.backcolor = Color.Green Then
            sender.backcolor = Color.LightGray
        End If
    End Sub

But there are ALOT of labels (about more than 300) and typing them out (or selecting them in the menu) consumes alot of time. Is there anyway to put the rest of them? I have about 8 groupboxes with these labels that need to be changed (there is another one that doesnt need this) so they are labels inside of groupboxes.

How can I do this?

Hey

I currently have

Private Sub LabelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label14.Click, Label15.Click, Label16.Click, Label17.Click

        If sender.backcolor = Color.LightGray Then
            sender.backcolor = Color.Green
        ElseIf sender.backcolor = Color.Green Then
            sender.backcolor = Color.LightGray
        End If
    End Sub

But there are ALOT of labels (about more than 300) and typing them out (or selecting them in the menu) consumes alot of time. Is there anyway to put the rest of them? I have about 8 groupboxes with these labels that need to be changed (there is another one that doesnt need this) so they are labels inside of groupboxes.

How can I do this?

So noone knows how to do this or there simply is no way?

>Is there anyway to put the rest of them?

....
 For Each cnt As Control In Me.Controls
            If TypeOf cnt Is GroupBox Then
                For Each child In cnt.Controls
                    If TypeOf child Is Label Then
                        CType(child, Label).BackColor = Color.Red
                    End If
                Next
            End If
        Next
..

>Is there anyway to put the rest of them?

....
 For Each cnt As Control In Me.Controls
            If TypeOf cnt Is GroupBox Then
                For Each child In cnt.Controls
                    If TypeOf child Is Label Then
                        CType(child, Label).BackColor = Color.Red
                    End If
                Next
            End If
        Next
..

But this will only work if I click one label right?
Lets say I have
1 2 3
all three are with background color white. When I click, I want it to change to red and if I click again back to white. But this for alot of labels.

>But this will only work if I click one label right?

No. It works for all labels in all the groupboxes on a form.

>But this will only work if I click one label right?

No. It works for all labels in all the groupboxes on a form.

So therefore the code would be:

Private Sub LabelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label14.Click, Label15.Click, Label16.Click, Label17.Click  
For Each cnt As Control In Me.Controls    
        If TypeOf cnt Is GroupBox Then   
             For Each child In cnt.Controls           
         If TypeOf child Is Label Then           
             CType(child, Label).BackColor = Color.Red      
              End If               
 Next           
 End If      
  Next
    End Sub

And it would work for the groupboxes that I require for this action to happen and all the labels in them? There are other groupboxes with labels that I do NOT want this to happen....

If you can give me a small explanation on the code, thank you because I dont understand the "cnt" part of the code.

>POST #1 - so they are labels inside of groupboxes.

For Each cnt As Control In Me.Controls  
..

All controls added into Form's collection (Label,TextBoxe, etc) are subclasses of Control class.

Me is an object variable has reference of activeform. Me.Controls is a property of Form has references of all controls (collection) added/placed into a form. For Each statement iterates Me.Controls collection by assigning reference of each control to the cnt - Control class variable.

If TypeOf cnt Is GroupBox Then 
...

TypeOf and IS operator check the datatype/classtype of an object which is currently referenced by the cnt variable.

I hope this will help you to understand code-snippet.

>POST #1 - so they are labels inside of groupboxes.

For Each cnt As Control In Me.Controls  
..

All controls added into Form's collection (Label,TextBoxe, etc) are subclasses of Control class.

Me is an object variable has reference of activeform. Me.Controls is a property of Form has references of all controls (collection) added/placed into a form. For Each statement iterates Me.Controls collection by assigning reference of each control to the cnt - Control class variable.

If TypeOf cnt Is GroupBox Then 
...

TypeOf and IS operator check the datatype/classtype of an object which is currently referenced by the cnt variable.

I hope this will help you to understand code-snippet.

Thanks for the help first off :)

I believe you send all the objects inside the forums into cnt and make all the labels turn from white to red and red to white, but there are only specific wants I want. Lets say I want label18-label20 and label25-label27

I dont want to have to put:

Private Sub LabelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label18.Click, Label19.Click, Label20.Click, Label25.Click,Label26.Click,Label27.Click

All these labels are in groupboxes (in my project).....I might not be understanding you or visaversa. So one more time, if you dont mind explaining your code again if it does what Id like. Thanks for all the help.

So you want to set background color for selected labels. Prepare an array/list of selected labels.

Dim lst(3) as Label
 lst(0)=Label18
 lst(1)=Label19
 ....
 For I=0 To lst.GetUpperBound(0)
    lst(i).BackColor=Color.Red 'Put your code here...
 Next

So you want to set background color for selected labels. Prepare an array/list of selected labels.

Dim lst(3) as Label
 lst(0)=Label18
 lst(1)=Label19
 ....
 For I=0 To lst.GetUpperBound(0)
    lst(i).BackColor=Color.Red 'Put your code here...
 Next

Well in the list I would have to include the names of all the labels right? So I would have to type them out anyways....


The only way Ive thought of (thanks to your idea) is to go thru the groupbox with the labels inside them that I specifically want their labels' background color to change when I click on them and add all their object names in the list with a for. Would this be possible?

But even then, what would I do on handling the event?

I know my problem is difficult and actually may be impossible, but I hope Ive explained it well. If not, please ask.

Thanks for all the help given already :)

I imagine this means it is impossible to do :(

Here is what you can do.
First, change the name of the labels that you want to be able to change background color into something that's easy to monitor, like lblBg1, lblBg2 etc...
When that's done you can use adaposts code for looping through the controls.

....
 For Each cnt As Control In Me.Controls
            If TypeOf cnt Is GroupBox Then
                For Each child In cnt.Controls
                    If TypeOf child Is Label Then
                        If CType(child, Label).Name.StartsWith("lblBg") Then
                            If CType(child, Label).BackColor = Color.Red Then
                                CType(child, Label).BackColor = Color.White
                            Else
                                CType(child, Label).BackColor = Color.Red
                            End If
                        End If
                    End If
                Next
            End If
        Next
..
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.