Wednesday, August 6, 2008

Change the height of a PropertyGrid's description area

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;
}
}

private void Form1_load(object sender, System.EventArgs e)
{ . . .
ResizeDescriptionArea(ref grid1, 6);
}

No comments: