Click here to Skip to main content
Licence CPOL
First Posted 28 Jul 2008
Views 25,640
Bookmarked 17 times

Change the height of a PropertyGrid's description area

By | 28 Jul 2008 | Article
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:

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

License

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

About the Author

Geno Carman



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionMaximized Form PinmemberScott 9710:49 10 Apr '12  
QuestionOK, but can you GET the height? PinmemberGraham Downs21:20 23 Feb '10  
AnswerRe: OK, but can you GET the height? PinmemberGeno Carman4:51 25 Feb '10  
GeneralRe: OK, but can you GET the height? PinmemberGraham Downs20:16 25 Feb '10  
GeneralRe: OK, but can you GET the height? PinmemberGeno Carman13:17 26 Feb '10  
GeneralRe: OK, but can you GET the height? PinmemberGraham Downs19:27 28 Feb '10  
GeneralThere is an error in the sample code PinmemberMember 15194043:44 20 Jan '10  
GeneralRe: There is an error in the sample code PinmemberGeno Carman4:56 25 Feb '10  
GeneralGet"ControlCollection"Fail Pinmemberelvis_pan18:09 22 Nov '08  
AnswerRe: Get"ControlCollection"Fail PinmemberGeno Carman5:56 23 Nov '08  
GeneralRe: Get"ControlCollection"Fail Pinmemberelvis_pan17:22 23 Nov '08  
GeneralRe: Get"ControlCollection"Fail PinmemberGeno Carman17:36 23 Nov '08  
GeneralRe: Get"ControlCollection"Fail Pinmemberelvis_pan20:23 23 Nov '08  
GeneralRe: Get"ControlCollection"Fail PinmemberGeno Carman14:58 24 Nov '08  
GeneralRe: Get"ControlCollection"Fail Pinmemberelvis_pan19:54 25 Nov '08  
GeneralRe: Get"ControlCollection"Fail Pinmemberjulian_perrott7:00 17 Mar '09  
Generalhi PinmemberMember 434214215:42 6 Aug '08  
GeneralRe: hi PinmemberGeno Carman18:54 6 Aug '08  
GeneralRe: hi PinmemberMember 434214215:03 11 Aug '08  
GeneralRe: hi PinmemberGeno Carman15:29 12 Aug '08  
GeneralRe: hi Pinmemberjmt9n5:14 29 Aug '08  
GeneralRe: hi PinmemberGeno Carman9:34 29 Aug '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 28 Jul 2008
Article Copyright 2008 by Geno Carman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid