Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Change the height of a PropertyGrid's description area

3.60/5 (4 votes)
28 Jul 2008CPOL 1  
How to change the height of a PropertyGrid's description area.

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:

C#
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:

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)