Click here to Skip to main content
15,884,629 members
Articles / Desktop Programming / Win32
Article

Change the height of a PropertyGrid's description area

Rate me:
Please Sign up or sign in to vote.
3.60/5 (4 votes)
28 Jul 2008CPOL 65.2K   18   24
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionAccess Controls property directly Pin
Daniele Rota Nodari31-May-22 1:29
Daniele Rota Nodari31-May-22 1:29 
QuestionMaximized Form Pin
Scott 9710-Apr-12 10:49
Scott 9710-Apr-12 10:49 
AnswerRe: Maximized Form Pin
Member 15069666-Mar-14 12:19
Member 15069666-Mar-14 12:19 
QuestionOK, but can you GET the height? Pin
Graham Downs23-Feb-10 21:20
Graham Downs23-Feb-10 21:20 
AnswerRe: OK, but can you GET the height? Pin
Geno Carman25-Feb-10 4:51
Geno Carman25-Feb-10 4:51 
GeneralRe: OK, but can you GET the height? Pin
Graham Downs25-Feb-10 20:16
Graham Downs25-Feb-10 20:16 
GeneralRe: OK, but can you GET the height? Pin
Geno Carman26-Feb-10 13:17
Geno Carman26-Feb-10 13:17 
GeneralRe: OK, but can you GET the height? Pin
Graham Downs28-Feb-10 19:27
Graham Downs28-Feb-10 19:27 
GeneralThere is an error in the sample code Pin
Member 151940420-Jan-10 3:44
Member 151940420-Jan-10 3:44 
GeneralRe: There is an error in the sample code Pin
Geno Carman25-Feb-10 4:56
Geno Carman25-Feb-10 4:56 
GeneralGet"ControlCollection"Fail Pin
elvis_pan22-Nov-08 18:09
elvis_pan22-Nov-08 18:09 
AnswerRe: Get"ControlCollection"Fail Pin
Geno Carman23-Nov-08 5:56
Geno Carman23-Nov-08 5:56 
GeneralRe: Get"ControlCollection"Fail Pin
elvis_pan23-Nov-08 17:22
elvis_pan23-Nov-08 17:22 
GeneralRe: Get"ControlCollection"Fail Pin
Geno Carman23-Nov-08 17:36
Geno Carman23-Nov-08 17:36 
GeneralRe: Get"ControlCollection"Fail Pin
elvis_pan23-Nov-08 20:23
elvis_pan23-Nov-08 20:23 
GeneralRe: Get"ControlCollection"Fail Pin
Geno Carman24-Nov-08 14:58
Geno Carman24-Nov-08 14:58 
GeneralRe: Get"ControlCollection"Fail Pin
elvis_pan25-Nov-08 19:54
elvis_pan25-Nov-08 19:54 
GeneralRe: Get"ControlCollection"Fail Pin
julian_perrott17-Mar-09 7:00
julian_perrott17-Mar-09 7:00 
Generalhi Pin
Member 43421426-Aug-08 15:42
Member 43421426-Aug-08 15:42 
GeneralRe: hi Pin
Geno Carman6-Aug-08 18:54
Geno Carman6-Aug-08 18:54 
GeneralRe: hi Pin
Member 434214211-Aug-08 15:03
Member 434214211-Aug-08 15:03 
GeneralRe: hi Pin
Geno Carman12-Aug-08 15:29
Geno Carman12-Aug-08 15:29 
GeneralRe: hi Pin
jmt9n29-Aug-08 5:14
jmt9n29-Aug-08 5:14 
GeneralRe: hi Pin
Geno Carman29-Aug-08 9:34
Geno Carman29-Aug-08 9:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.