Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 30-Jan-13 23:59pm
Edited 31-Jan-13 2:27am
v2
Comments
ryanb31 at 31-Jan-13 8:02am
   
Color is just a property on the label. Where are you stuck?
saurabh kumar mahto at 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

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

Solution 1

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
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
        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.
  Permalink  
Comments
saurabh kumar mahto at 31-Jan-13 9:05am
   
Thanks for your helpful suggestions..
you are a genius.
Fred Flams at 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
0 Maciej Los 450
1 OriginalGriff 403
2 CPallini 189
3 Sergey Alexandrovich Kryukov 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,958
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
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