Background
I recently created a tool that used a PropertyGrid
. The property descriptions tended to be very long, and would not fit in the control's description area. I decided to change the description height at runtime, since I couldn't figure out how to do it at design time. Since I had no clue how to proceed (my first C# project and my first time using Visual Studio 7.1), I searched the web for existing solutions. I failed to find any C# solution on the Web, but I did find a VB article on Matthew Cosier’s blog, and adapted that code to my purposes.
Using the code
Simply insert the function below into your code and call it:
Private bool ResizeDescriptionArea(ref PropertyGrid grid, int nNumLines)
{
try
{
System.Reflection.PropertyInfo pi = grid.GetType().GetProperty("Controls");
Control.ControlCollection cc = (ControlCollection)pi.GetValue(grid, null);
foreach(Control c in cc)
{
Type ct = c.GetType();
string sName = ct.Name;
if(sName == "DocComment")
{
pi = ct.GetProperty("Lines");
pi.SetValue(c, nNumLines, null);
System.Reflection.FieldInfo fi = ct.BaseType.GetField("userSized",
System.Reflection.BindingFlags.Instance,
System.Reflection.BindingFlags.NonPublic);
fi.SetValue(c, true);
}
}
return true;
}
catch(Exception error)
{
#if(DEBUG)
MessageBox.Show(error.Message, "ResizeDescriptionArea()");
#endif
return false;
}
}
I call it like this:
private void Form1_load(object sender, System.EventArgs e)
{
.
.
.
ResizeDescriptionArea(ref grid1, 6);
}