Basic use of PropertyGrid in C# WinForm programming
SeBesides my own engine, I have developed an editor with C# and CLI/C++. PropertyGrid is a good control to set and retrieve object properties.
(Editor with PropertyGrid panel on the right)
To bind an object to PropertyGrid, just set SelectedObject on PropertyGrid to your object:
propertyGrid1.SelectedObject = myGameObject;
To have properties shown up on the grid, object class must have accessor for the property. A mutator is optional when property is read-only to user.
PropertyGrid is very flexible with basic data types and string. For a structural data such as vector3, a good idea is to convert data to string for display/modification, and store to vector3 from string value. Let’s take property position as an example:
property String^ Position
{
String^ get()
{
return Vec3ToString(m_SceneObject->GetPosition());
}
void set(String^ value)
{
m_SceneObject->SetPosition(StringToVec3(value));
}
};
Both accessor and mutator are used to make change to the property in PropertyGrid.
Here are the convertor functions between Vector3 and String:
String^ ManagedSceneObject::Vec3ToString(const RVec3& vec)
{
return String::Format(L"{0}, {1}, {2}", vec.x, vec.y, vec.z);
}
RVec3 ManagedSceneObject::StringToVec3(String^ str)
{
String^ delimStr = ",";
array<Char>^ delimiter = delimStr->ToCharArray();
array<String^>^ words = str->Split(delimiter);
RVec3 vec;
vec.x = (float)Convert::ToDouble(words[0]);
vec.y = (float)Convert::ToDouble(words[1]);
vec.z = (float)Convert::ToDouble(words[2]);
Now, when showing selected object’s property, the position will be shown as a comma-separated string. Input any of three components of vector and object will reflect the change immediately.