Visual Studio 2015 C# color dialog for textbox when clicked in

2k views Asked by At

So I'm trying to create a small program where when you click in an empty text box a dialog box will appear and change the background of the text box. Now I've tried to use the bellow code but it does nothing. The textbox is in read-only mode. Any help is very appreciated.

 private void textBox1_Enter(object sender, System.EventArgs e)
    {
        colorDialog1.ShowDialog();
        textBox1.BackColor = colorDialog1.Color;

    }
1

There are 1 answers

2
Flydog57 On BEST ANSWER

(if you are using Windows Forms (which I'm assuming you are), you should tag it, and you shouldn't have a "Visual Studio" tag)

If I create a Windows Forms app, drop a textbox and a Color Dialog on the form, and add this code:

    private void textBox1_Enter(object sender, EventArgs e)
    {
        if (colorDialog1.ShowDialog() == DialogResult.OK)
        {
            textBox1.BackColor = colorDialog1.Color;
        }
    }

...up pops the Color Dialog. If I pick a color and press OK, the back color of the text box changes. I tried this with and without ReadOnly set to true (I was surprised you can "Enter" a read-only text box, but...).

So, are you seeing the color dialog pop up and you select a color and press OK and then you see nothing? That's surprising. Or, are you seeing something else?

For what it's worth, this is not a great UI design. I don't expect another dialog to pop up as the result of clicking in a textbox. Also note that this may only happen once. Once you have clicked in the text box, you have entered it. You have to set the focus on another control before clicking in the text box again. If you click the same place over an over, you only enter it the first time.