簡體   English   中英

使用PropertyGrid更改shapes屬性

[英]Changing shapes property with PropertyGrid

我有一個帶有三個值(圓形,矩形和直線)的組合框:

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是來自circle,rectangle和line類的新對象。我將這些形狀打印在面板上,並且希望能夠通過PropertyGrid更改其屬性(例如更改圓形顏色)。 我已經嘗試過類似的東西:

private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
    switch(propertyGrid1.SelectedGridItem.ToString())
    {
        case GridItem=Color
            {

            }
            .
            .
            .
    }

}

但是我不知道該怎么做。 你能幫我嗎?

您應該具有一些包含位置和顏色等屬性的形狀。 然后在出現PictureBoxPanel之類的控件的Paint事件中,繪制形狀。 當使用編輯形狀PropertyGrid ,這足以應付PropertyValueChanged的事件PropertyGrid並調用Invalidte於紙面控制的方法。

能有這樣的形狀,用我在這里創造的形狀這篇文章 ,並使用這些事件:

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);
}

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM