我是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);
}
但我知道这不对。我怎么想这样做?
答案 0 :(得分:1)
你可以做的是首先填充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;
}
}