how do you get a color from a text box?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jprevails
    New Member
    • Mar 2010
    • 3

    how do you get a color from a text box?

    I am very new to programming, and I need to learn how to get a color from a text box:

    Color color = Color.Parse(tex tBoxNewValue.Te xt);
    //set the Color field to the new value
    box.setColor(co lor);
    //put the updated box back in the list
    //to relect the change
    boxlist[index] = box;

    this works for me with a decimal, but I cannot find the proper code to do it for a color, Please help?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    A text box actually has two color properties: backcolor and forecolor.

    You get them just like you get the text value.

    Code:
    string myString = myTextBox.Text;
    color myBackgroundcolor = myTextbox.BackColor;
    color myTextColor = myTextBox.Forecolor;

    Comment

    • Jprevails
      New Member
      • Mar 2010
      • 3

      #3
      Thank you for responding

      I am not sure I made my question clear though, I need to get a user entered color (text such as 'Blue') from the text box.
      so I don't think the back or foreground colors apply.

      I could of course be wrong, my brother is teaching me and he says I always overthink my problems

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        OHhh... Sorry. I misunderstood.

        Do you really trust the user to enter the color by name in a textbox?
        What if they enter 'grey' instead of 'gray' ?

        What do you think of a combobox that you can pre-populate with a list of all the available colors? Then they pick from the list?

        Otherwise, you will have to take their text and loop through the list of KnownColor for a match.

        Comment

        • Jprevails
          New Member
          • Mar 2010
          • 3

          #5
          Thank you! this whas just what I needed!


          " Otherwise, you will have to take their text and loop through the list of KnownColor for a match. "

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            You didn't want to use the ColorDialog ?

            Comment

            Working...