Click here to Skip to main content
15,896,154 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.4K   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 
I think I passed the right argument. I post the code and you try it.The simple code as follow:
The Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace wpropertygrid
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            
        }

        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 button1_Click(object sender, EventArgs e)
        {
            ResizeDescriptionArea(ref propertyGrid1, 20);

        }
    }
    
}

The Form2.Designer.cs
namespace wpropertygrid
{
    partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // propertyGrid1
            // 
            this.propertyGrid1.Location = new System.Drawing.Point(36, 42);
            this.propertyGrid1.Name = "propertyGrid1";
            this.propertyGrid1.Size = new System.Drawing.Size(130, 130);
            this.propertyGrid1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(189, 73);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.propertyGrid1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PropertyGrid propertyGrid1;
        private System.Windows.Forms.Button button1;
    }
}

The Program.cs:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace wpropertygrid
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());            
        }       

    }
}

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.