Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

set color to a label

Posted on 2002-05-21
6
Medium Priority
?
672 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 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

604 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