?
Solved

set color to a label

Posted on 2002-05-21
6
Medium Priority
?
658 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Accepted Solution

by:
andrejaa earned 400 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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