简体   繁体   English

单击时的文本框的Visual Studio 2015 C#颜色对话框

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

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;

    }

(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) (如果您使用的是Windows窗体(我假设您使用的是Windows窗体),则应该对其进行标记,并且不应该带有“ Visual Studio”标记)

If I create a Windows Forms app, drop a textbox and a Color Dialog on the form, and add this code: 如果我创建Windows窗体应用程序,请在窗体上放置一个文本框和一个“颜色对话框”,然后添加以下代码:

    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. 如果我选择一种颜色并按OK,则文本框的背面颜色会更改。 I tried this with and without ReadOnly set to true (I was surprised you can "Enter" a read-only text box, but...). 我在没有将ReadOnly设置为true的情况下尝试了此操作(我很惊讶您可以“输入”一个只读文本框,但是...)。

So, are you seeing the color dialog pop up and you select a color and press OK and then you see nothing? 因此,您是否看到弹出的颜色对话框,然后选择一种颜色并按OK,然后什么都看不到? That's surprising. 真令人惊讶 Or, are you seeing something else? 或者,您看到其他东西了吗?

For what it's worth, this is not a great UI design. 就其价值而言,这不是一个很棒的UI设计。 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. 如果您单击上方的同一地点,则只会在第一次输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM