Convert string to Color in C#

后端 未结 10 1402
我寻月下人不归
我寻月下人不归 2020-11-29 07:03

I am encountering a problem which is how do I convert input strings like \"RED\" to the actual Color type Color.Red in C#. Is there a good way to do this?

10条回答
  •  无人及你
    2020-11-29 07:28

    The simplest way:

    string input = null;
    Color color = Color.White;
    
    TextBoxText_Changed(object sender, EventsArgs e)
    {
       input = TextBox.Text;
    }
    
    Button_Click(object sender, EventsArgs e)
    {
       color = Color.FromName(input)
    }
    

提交回复
热议问题