2016-11-09 31 views
0

我有三個值(圓形,矩形,和線路)該組合框:變遷形狀屬性與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是從圓形,矩形和線class.I新對象具有這些形狀印在我的面板上,我希望能夠通過PropertyGrid(如更改圓圈顏色)更改其屬性。我試過類似的東西:

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

      } 
      . 
      . 
      . 
    } 

} 

但我不知道如何正確地做到這一點。你能幫助我嗎?

+0

我建議應用按鈕。當您在組合框中選擇一個形狀時,將其屬性加載到propertyGrid並讓用戶修改它們,然後他自己單擊該按鈕,該按鈕將採用屬性並重新繪製形狀。 – Poody

+0

該按鈕應該如何工作? – sara

+0

那麼,在使用_PropertyValueChanged進行編輯的過程中,您不必閱讀propertyGrid,而是通過單擊按鈕一次全部閱讀它們。用戶在完成屬性的編輯之後將點擊按鈕。 – Poody

回答

0

你應該有一些包含位置和顏色等屬性的形狀。然後在PaintPictureBoxPanel這樣的控件事件中繪製您的形狀。使用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); 
} 

enter image description here