【问题标题】:Changing shapes property with PropertyGrid使用 PropertyGrid 更改形状属性
【发布时间】:2016-11-09 07:03:17
【问题描述】:

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

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
            {

            }
            .
            .
            .
    }

}

但我不知道如何正确执行此操作。你能帮我解决这个问题吗?

【问题讨论】:

  • 我建议使用应用按钮。当您在组合框中选择一个形状时,将其属性加载到 propertyGrid 并让用户修改它们,然后他自己单击按钮,该按钮将获取属性并重绘形状。
  • 这个按钮应该如何工作?
  • 好吧,不是在编辑过程中使用 _PropertyValueChanged 读取 propertyGrid,而是在单击按钮时一次读取它们。用户在完成属性编辑后单击按钮。

标签: c# .net winforms gdi+ propertygrid


【解决方案1】:

您应该有一些包含位置和颜色等属性的形状。然后在像PictureBoxPanel 这样的控件的Paint 事件中,绘制你的形状。当使用PropertyGrid编辑你的形状时,处理PropertyGridPropertyValueChanged事件并调用绘图表面控件的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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多