Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Colors at Runtime 1

Status
Not open for further replies.

cortastic

Programmer
Nov 4, 2008
3
This should be a simple question! I am attempting to use a series of comboboxes to change the forecolor of a series of labels during runtime.

So far, the only method I have found that works is the tedious method:

Private Sub ComboBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.Text
Case "Green"
lbl1.ForeColor = Color.Green
Case "Blue"
lbl1.ForeColor = Color.Blue
Case "Black"
lbl1.ForeColor = Color.Black
Case "Red"
lbl1.ForeColor = Color.Red
End Select
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Select Case ComboBox2.Text
Case "Green"
lbl2.ForeColor = Color.Green
Case "Blue"
lbl2.ForeColor = Color.Blue
Case "Black"
lbl2.ForeColor = Color.Black
Case "Red"
lbl2.ForeColor = Color.Red
End Select
End Sub

I'd prefer to

a) not have to type all this code
b) have the program add items to the comboboxes for me or recognize the combobox values so I don't have to even worry about populating the comboboxes with a list.

I tried doing this:

Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
lbl1.ForeColor = System.Drawing.Color.FromName(ComboBox1.SelectedText)
End Sub

It doesn't return any errors, thankfully, but it also doesn't change the label color.

I have taught myself visual basic, so I'm sure I'm just missing something basic. I'd be truly appreciative of any suggestions and the opportunity to learn something new!
 
Hmmmm... I wasn't aware there was such a thing. (as I am a newbie). After googling it, I can see how this would be helpful!

One of the things I have this program set up to do, however, is to save the values of the comboboxes into a record in a database, so that I can load the color profile at a later point.

Could I still insert values from the ColorDialog into a database or would it be more trouble than its worth?
 
Yes. you can save the values for later use.

"...and did we give up when the Germans bombed Pearl Harbor? NO!"
"Don't stop him. He's roll'n.
 
Excellent! Time to do some googling. Thank you gentlemen!!
 
Though the color dialog is the best solution, here is how you do it with a selection from a listbox. It also shows how to load known colors to listbox.

Code:
Imports System.Drawing
' one label control
' two listboxes

Public Class Form1
    Dim kColor As KnownColor

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For kColor = KnownColor.AliceBlue To KnownColor.YellowGreen
            lstColor1.Items.Add(kColor)
            lstColor2.Items.Add(kColor)
        Next
    End Sub


    Private Sub lstColor1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstColor1.SelectedIndexChanged
        Me.Label1.BackColor = Color.FromName(Me.lstColor1.SelectedItem.ToString)
    End Sub

    Private Sub lstColor2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstColor2.SelectedIndexChanged
        Me.Label1.ForeColor = Color.FromName(Me.lstColor2.SelectedItem.ToString)
    End Sub
End Class

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top