首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PropertyGrid更改形状属性

使用PropertyGrid更改形状属性
EN

Stack Overflow用户
提问于 2016-11-09 15:03:17
回答 1查看 132关注 0票数 0

我有一个包含三个值(圆形、矩形和线条)的组合框:

代码语言:javascript
复制
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更改它们的属性(如更改圆的颜色)。我尝试过这样的方法:

代码语言:javascript
复制
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
    switch(propertyGrid1.SelectedGridItem.ToString())
    {
        case GridItem=Color
            {

            }
            .
            .
            .
    }

}

但我不知道该怎么做。你能帮我做这个吗?

EN

回答 1

Stack Overflow用户

发布于 2016-11-14 01:19:17

你应该有一些包含属性的形状,比如位置和颜色。然后在PictureBoxPanel等控件的情况下,绘制您的形状。在使用PropertyGrid编辑形状时,只需处理PropertyGridPropertyValueChanged事件并调用绘图图面控件的Invalidte方法即可。

示例

要获得这样的形状,请使用我在this post中创建的形状,并使用以下事件:

代码语言:javascript
复制
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);
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40501577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档