Solved

set color to a label

Posted on 2002-05-21
6
610 Views
Last Modified: 2012-08-14
Hi,
I have a form with 5 labels on it. The caption is 1,2,3,4,5. I also have a listbox on the form with
2 columns and 5 records. The first column is a number
1 to 5 and the second column has a value of either P
or C like this.
1  P
2  C
3  P
4  P
5  C
What i want is to change the back color of all labels
where the second column is P.
Label 1 red, label 3 red and label 4 red all others should remain green.
Any help is appreciated
Manny
0
Comment
Question by:MannyN
6 Comments
 
LVL 11

Expert Comment

by:joekendall
ID: 7025228
You would do something like this (assuming you have a listbox called ListBox1):

If ListBox1.Column(1) = "P" Then
    Label1.BackColor = 255 '255 is Red
    Label3.BackColor = 255
    Label4.BackColor = 255
End If

I think that will give you the concept.

Thanks!

Joe
0
 

Author Comment

by:MannyN
ID: 7025412
Joe, I've been that far to change the color of the label
but the records are changeing. Next time label 1 has a C
then the back color should not be red.
I think it should be someting like
If Me!List48.Column(0)= "1" and Me!List48.Column(1)="P" Then
Label1.Backcolor = 255
else
End If
That changes my color right but only for the first item in the list box not for all records.
0
 
LVL 4

Expert Comment

by:zuijdhoek
ID: 7026984
MannyN,

If you want to change the colors of the labels each time you change item in the listbox, you should use the AfterUpdate-event of your listbox-control.

Private Sub ListBox1_AfterUpdate()
    If ListBox1.Column(1) = "P" Then
       label1.BackColor = vbRed
       label2.BackColor = vbGreen
       label3.BackColor = vbRed
       label4.BackColor = vbRed
       label5.BackColor = vbGreen
    Else
       label1.BackColor = vbGreen
       label2.BackColor = vbGreen
       label3.BackColor = vbGreen
       label4.BackColor = vbGreen
       label5.BackColor = vbGreen
    End If
End Sub

Hope this might help you out,

Mark
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 1

Accepted Solution

by:
andrejaa earned 100 total points
ID: 7027282
Hi MannyN ,
here is function which you should run every time when
data in your listbox are changed .

----------code begin -----------------
Function ChgLabelBColor()

'this function should be called every time when
'you change values in your listbox

Dim row As Variant
Dim r_value As Variant

'enumerate all rows in list box
'and run through loop to check second column
'values in each row

For i = 0 To lstbox.ListCount - 1
    row = lstbox.Column(0, i) '1st column value
    r_value = lstbox.Column(1, i) '2nd column value
    'let asume that you had follow some rules
    'in assigning labels names
    'ie . L1 - L2 - L3 -L4 -L5 ......
    Select Case r_value
    'check the value of second column
    '( P = vbRed and C = vbGreen )
        Case "P"
            Me("L" & row).BackColor = vbRed
        Case "C"
            Me("L" & row).BackColor = vbGreen
        Case Else
            'if there is error
            '(<> P or <> C , make back color yellow)
             Me("L" & row).BackColor = vbYellow
    End Select
Next i

End Function
----------code end -----------------
0
 
LVL 11

Expert Comment

by:joekendall
ID: 7027426
Try this code in the form's code module. You should have a Current event for the form.

Private Sub Form_Current()

If ListBox1.Column(1) = "P" Then
   Label1.BackColor = vbRed 'Or use 255
   Label3.BackColor = vbRed
   Label4.BackColor = vbRed
Else
   Label1.BackColor = vbGreen
   Label3.BackColor = vbGreen
   Label4.BackColor = vbGreen
End If

End Sub

This should do the trick. For each record that you go to, the color will change based on the value in the ListBox.

Thanks!

Joe

0
 

Author Comment

by:MannyN
ID: 7027431
Andrejaa, Thanks for your help.
Works like a charm.
Manny
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now