Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a client_status user control in vb.net,
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 vb.net 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.
lblClientStatusColor0,
lblClientStatusColor1,
lblClientStatusColor2...

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
SetStatusAvailable(i)
Case Is = ClientStatus.Connected
SetStatusConnected(i)
Case Is = ClientStatus.ConnectionLost
SetStatusConnectionLost(i)
Case Is = ClientStatus.Ready
SetStatusReady(i)
Case Is = ClientStatus.TLError
SetStatusTLError(i)

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
Updated 31-Jan-13 2:27am
v2
Comments
ZurdoDev 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..?
lblClientStatusColor0,
lblClientStatusColor1,
lblClientStatusColor2,
lblClientStatusColor3...

1 solution

Hello,

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
VB
Me.BackColor = aColor


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

Solution B
VB
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
    Next
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.
 
Share this answer
 
Comments
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)



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