Click here to Skip to main content
11,922,641 members (32,065 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
I have a client_status user control in,
with labels named Connected,Available,Not Available,Ready,Connection Lost
I want to set status of connection with color of lable.
If status is connected ,label should show green else red color.
similarly for all other options...
Can anyone please give me any Idea how to do this in or any code snippet ?

Thanks in advance

here I am putting some code snippet to get my que clear..

How can I iterate through all the labels, for e.g.

Code Below:

Private Sub timClientStatus_Timer()
If gIsTerminating = False Then
Dim i As Integer
For i = 0 To gMaxClientSystem - 1
'Client status
Select Case gClientStatus(i)
Case Is = ClientStatus.NotAvailable
SetStatusNotAvailable(i)''--- Subroutine defined below
Case Is = ClientStatus.Available
Case Is = ClientStatus.Connected
Case Is = ClientStatus.ConnectionLost
Case Is = ClientStatus.Ready
Case Is = ClientStatus.TLError

End Select

''Channel setup status
lblChannelSetupStatus(i).Caption = gChannelSetupStatus(i)

''Env status
lblEnvString(i).Caption = gEnvStatus(i)

Next i
End If

End Sub

Private Sub SetStatusNotAvailable(vClientNr As Integer)

''---- Change UI
lblClientStatusColor0.BackColor = Color.White
lblClientStatus0.Text = "Not Available" ''(vClientNr).Caption = Available
lblClientStatus0.Enabled = False
chk_ClientSelection0.Enabled = False

lblChannelSetupStatusColor0.BackColor = Color.White
lblChannelSetupStatus0.BackColor = Color.White
lblChannelSetupStatus0.Enabled = False
lblEnvString0.BackColor = Color.White
lblEnvString0.Enabled = False

End Sub
Posted 31-Jan-13 0:59am
Edited 31-Jan-13 3:27am
ryanb31 31-Jan-13 8:02am
Color is just a property on the label. Where are you stuck?
saurabh kumar mahto 31-Jan-13 8:49am
Thanks Ryan for your reply..
I an using 4 Labels for 4 Client's status.
and i want to loop all this lables..?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


There exists two approaches to that king of problem:
A) Change the backcolor of the whole custom control (very easy)
B) If you really need to change only the color of the labels, iterate through the Controls collection and change the backcolor of the items of type Label

Solution A
Me.BackColor = aColor

Then put that line of code in each "SetStatus" method replacing "aColor" with the correct Color structure.

Solution B
Private Sub SetLabelColor(aColor As Color)
    Dim aControl As Control
    For Each aControl In Me.Controls
        If TypeOf (aControl) Is Label Then
            aControl.BackColor = aColor
        End If
End Sub
Then put a call to that method in each "SetStatus" method passing the color as a parameter.

Solution B works only for labels directly held by your user control, if they are placed inside another container (say for example a panel), then you will need to enumerate through that subcontrol "Controls" collection and change the label colors approprietally to your need.

Hope my comments will help you.
saurabh kumar mahto 31-Jan-13 9:05am
Thanks for your helpful suggestions..
you are a genius.
Fred Flams 31-Jan-13 9:16am
I'm not really a genius, more like an old dog having a big bag of tricks....

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151125.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100