I realise that this is a simple and probably daft question...
Here is my problem.

I am trying to create the VB code to change label background colors, it sounds simple, however i want a user to select from one of four lables one red/blue/green and yellow, then select from one of 4 buttons named Red/Blue/Green/Yellow and it will change the color of the lable that they originaly selected to that button choise...

I cannot for the life of me think as to how to store the selected value for the selected lable and thrn use it within the code to alter the colors via .backcolor

SM

Recommended Answers

All 7 Replies

Why you need to select the color using a label and why 4 buttons just to change the color.

Select the color using a combobox and use a single button.

I sort of agree with @debasisdas...seems like your approach is overly complicated.

However, if you are determined to do it the way you want to do it, try this:

Every time you click on one of the labels, store its name in the form's Tag property. Then, when you click on one of the buttons, have it reference the form's Controls collection indexed by the form's Tag property and set it to the appropriate color constant. Here's some code that might help:

Private Sub Label1_Click()
Me.Tag = "Label1"
End Sub
' ... more of your code here ...
Private Sub cmdChangeColorRed_Click()
Me.Controls(Me.Tag).BackColor = vbRed
End Sub

Of course, you'll have to set the color constants appropriately for each button, and the label name appropriately in each label's click event.

Hope this helps!

Why you need to select the color using a label and why 4 buttons just to change the color.

Select the color using a combobox and use a single button.

Well as you can probalbly see i am learning VB, and i was asked a question by a friend... that was to create a form with 4 lables, each a different color, then create 4 buttons with red/blue/yellow/green...
So..
The user selects a color from the colored labels by clicking on one of them, they then select a color from the buttons, i then change which ever lable they selected to the color they choose from the button selection.
Sounds daft i know....

label1.backcolor=rgbrnd255,rnd255,rnd*255)

you have two options
which color you will use in you form background

Private Sub Command1_Click()
Label1.BackColor = &HC0C000
End Sub

Private Sub Form_Load()
Label1.BackColor = vbRed

End Sub

label1.backcolor=rgb(255rnd,255rnd,255*rnd)

button1_click
label1.backcolor=vbred
button2_click
label1.backcolor=vbgreen
etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.