我有一个包含三个值(圆形、矩形和线条)的组合框:
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;
}
}C和l是圆、矩形和线条类中的新对象。我在面板上打印了这些形状,我希望能够通过PropertyGrid更改它们的属性(如更改圆的颜色)。我尝试过这样的方法:
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
switch(propertyGrid1.SelectedGridItem.ToString())
{
case GridItem=Color
{
}
.
.
.
}
}但我不知道该怎么做。你能帮我做这个吗?
发布于 2016-11-14 01:19:17
你应该有一些包含属性的形状,比如位置和颜色。然后在PictureBox或Panel等控件的情况下,绘制您的形状。在使用PropertyGrid编辑形状时,只需处理PropertyGrid的PropertyValueChanged事件并调用绘图图面控件的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);
}

https://stackoverflow.com/questions/40501577
复制相似问题