gpt4 book ai didi

c# - 根据从组合框中选择的颜色更改背景色

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:55 26 4
gpt4 key购买 nike

在我的表单中,我有一个组合框。它有红色、黄色等颜色名称。

我想更改表单的背景颜色,以匹配单击按钮时从组合框中选择的颜色。到目前为止我有这个:

private void button_Pass_Click(object sender, EventArgs e)
{
if (comboBox_color.SelectedText == "Red")
{
this.BackColor = System.Drawing.Color.Red;
}
else if (comboBox_color.SelectedText == "Yellow")
{
this.BackColor = System.Drawing.Color.Yellow;
}
else
{
this.BackColor = System.Drawing.Color.Blue;
}
}

当我单击该按钮时,窗体的 BackColor 始终设置为蓝色。我在这里做错了什么?

最佳答案

您可以使用 Color.FromName

this.BackColor = Color.FromName(comboBox_color.SelectedItem.ToString());

If the name parameter is not the valid name of a predefined color, the FromName method creates a Color structure that has an ARGB value of 0 (that is, all ARGB components are 0).

关于c# - 根据从组合框中选择的颜色更改背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30840952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com