2016-11-09 3 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

場所や色などのプロパティを含む図形が必要です。次にPictureBoxまたはPanelのようなコントロールのPaintイベントで、あなたの形を描画します。 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

関連する問題