我的这个组合框有三个值(圆形,矩形和直线):
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.SelectedItem.ToString())
{
case "circle":
{
propertyGrid1.SelectedObject = c;
}
break;
case "line":
{
propertyGrid1.SelectedObject = l;
}
break;
case "rectangle":
{
propertyGrid1.SelectedObject = r;
}
break;
default:
break;
}
}
r,c和l是来自圆形,矩形和线类的新对象。我有这些形状,打印在我的面板上,我希望能够通过PropertyGrid
更改其属性(就像更改圆形颜色一样) )。我尝试过类似的事情:
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
switch(propertyGrid1.SelectedGridItem.ToString())
{
case GridItem=Color
{
}
.
.
.
}
}
但我不知道如何正确地做到这一点。你可以帮帮我吗?
答案 0 :(得分:0)
你应该有一些包含位置和颜色等属性的形状。然后在Paint
或PictureBox
这样的控件的Panel
事件中,绘制您的形状。使用PropertyGrid
修改形状时,它足以处理PropertyValueChanged
PropertyGrid
事件并调用绘图表面控件的Invalidte
方法。
示例强>
要拥有此类形状,请使用我在this post创建的形状并使用这些事件:
ShapesList Shapes;
private void Form3_Load(object sender, EventArgs e)
{
Shapes = new ShapesList();
Shapes.Add(new RectangleShape() { Rectangle = new Rectangle(0, 0, 100, 100),
Color = Color.Green });
Shapes.Add(new RectangleShape() { Rectangle = new Rectangle(50, 50, 100, 100),
Color = Color.Blue });
Shapes.Add(new LineShape() { Point1 = new Point(0, 0), Point2 = new Point(150, 150),
Color = Color.Red });
this.panel1.Invalidate();
this.comboBox1.DataSource = Shapes;
}
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
this.panel1.Invalidate();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.propertyGrid1.SelectedObject = this.comboBox1.SelectedItem;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Shapes.Draw(e.Graphics);
}