如何使用WinForms和c#制作组合框
本文关键字:组合 何使用 WinForms | 更新日期: 2023-09-27 18:17:09
我是新来的WinForms
编码,我正试图使这个迷你油漆有3个按钮(circle
, line
和rectangle
)。这些是我的按钮:
private void circle_btn_Click(object sender, EventArgs e)
{
circle c = new circle() {startx=10,thickness=4,starty=10,radius=100,color=Color.Black };
shapes.Add(c);
panel1.Invalidate();
}
private void rectangle_btn_Click(object sender, EventArgs e)
{
rectangle r = new rectangle() { startx = 10,thickness=4, starty = 10, length = 200, width = 100, color = Color.Black };
shapes.Add(r);
panel1.Invalidate();
}
private void line_btn_Click(object sender, EventArgs e)
{
line l = new line() {startx=10,starty=10,thickness=4,endx=200,endy=200,color=Color.Black };
shapes.Add(l);
panel1.Invalidate();
}
当我点击它们时,一个随机形状将被打印在我的Panel
上。我想做一个ComboBox
,它有这三个值(圆,线和矩形),让我选择其中一个,以便在PropertyGrid
中改变它的属性。我试过这样写:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Button namestr = line_btn;
comboBox1.Items.Add(namestr);
}
但我知道这是不对的。我该怎么做呢?
您可以做的是首先填充您的ComboBox
。在构造函数中:
public Form1()
{
comboBox1.Items.AddRange(new List<string>() { "circle", "line", "rectangle"}.ToArray());
}
或者你已经取了一个数组:
public Form1()
{
comboBox1.Items.AddRange(new string []{ "circle", "line", "rectangle"});
}
并根据SelectedIndexChanged
事件中的值进行切换:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.SelectedItem.ToString())
{
case "circle":
//do something circular
break;
case "line":
//do something linish
break;
case "rectangle":
//do something edgy
break;
default:
break;
}
}