Solved

set color to a label

Posted on 2002-05-21
6
641 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

733 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