Reputation: 63
I am trying to make this mini paint with three buttons(line, circle and rectangle). on each button click , a related shape with random color and start point will be printed. I've made this combo box so that the user chooses a shape and see its properties in 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;
}
}
c,l and r are new objects from circle , line or rectangle class. now I want to be able to override properties using propertyGrid, like changing a circles color or startpoint. some thing like :
private void propertyGrid1_Click(object sender, EventArgs e)
{
circle.changeproperties=Griditem.value;
}
so how should I write this?
Upvotes: 1
Views: 1386
Reputation: 5737
The trick is: You don't have to do anything. By binding your object instance the PropertyGrid by setting propertyGrid1.SelectedObject = XXX
, you're already done.
The grid will automatically populate its editors to make it possible to edit that values. By changing them, the values will automatically be written back to the corresponding class instance.
Go try it and write a full blown property implementation in your classes like this
public int Radius
{
get { return _radius; }
set { _radius = value; }
}
and put a breakpoint in the setter. You'll see that it will get called if you change the value in the UI.
Anyway - now you got the values back in your class instance. Fine. But now you have to trigger your application to do anything with it. Typically, a control should invalidate itself now to trigger its painting again to make that change visible in the UI. Depending on your application logic, you might implement INotifyPropertyChanged
in your Circle, Rectangle and Line class and react on it. See here for more detail: Implementing INotifyPropertyChanged - does a better way exist?
Upvotes: 1