+ Reply to Thread
Results 1 to 2 of 2

Thread: colorDialog to place the text version of a color into a textbox

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    14

    colorDialog to place the text version of a color into a textbox

    How do you use the colorDialog to place the text version of a color into a textbox? I have a textbox and I fire the colorDialog upon clicking a button next to it. The string of the color goes into the text box, but the final result is of the form 'Color [Green]' and not simply 'Green'. Is there a better way to do this. My current code is as follows:

    Code:
    public partial class ColorTie : Form
    {
        public ColorTie()
        {
            InitializeComponent();
        }
    
        private void ColorTie_Load(object sender, EventArgs e)
        {
    
        }
    
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
    
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
    
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = colorDialog1.Color.ToString();
            }
        }
    
    }

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    15
    If I'm understanding your question correctly, I believe the property you're looking for is Color.Name.

    If your user selects red from your dialog, colorDialog1.Color.Name will return the string "Red". Beware though, because the user might select a color that doesn't have a Color.Name property value. To guard against this, you can check the Color.IsNamedColor property; if it's true you can use Color.Name like I described (otherwise you get back the RGB value).

    Something like this:
    Code:
    if (colorDialog1.ShowDialog() == DialogResult.OK) {
        if (colorDialog1.Color.IsNamedColor) {
            Console.WriteLine("Named color: {0}", colorDialog1.Color.Name);
        }
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts