4

I am using a .NET PropertyGrid control in my C# project.

When the form containing the grid loads, the horizontal splitter (which divides the Settings from the Description) is at a default position. How do I change the position of that splitter programmatically in C#?

John Rudy
  • 37,282
  • 14
  • 64
  • 100
PICyourBrain
  • 9,976
  • 26
  • 91
  • 136

4 Answers4

10

This code is based off of an article (http://www.codeproject.com/KB/grid/GridDescriptionHeight.aspx) from The Code Project, with two fixes and some cleanup introduced.

private void ResizeDescriptionArea(PropertyGrid grid, int lines)
{
    try
    {
        var info = grid.GetType().GetProperty("Controls");
        var collection = (Control.ControlCollection)info.GetValue(grid, null);

        foreach (var control in collection)
        {
            var type = control.GetType();

            if ("DocComment" == type.Name)
            {
                const BindingFlags Flags = BindingFlags.Instance | BindingFlags.NonPublic;
                var field = type.BaseType.GetField("userSized", Flags);
                field.SetValue(control, true);

                info = type.GetProperty("Lines");
                info.SetValue(control, lines, null);

                grid.HelpVisible = true;
                break;
            }
        }
    }

    catch (Exception ex)
    {
        Trace.WriteLine(ex);
    }
}

I've used it in my own projects; it should work fine for you.

  • Worked Great. I had to play with the values of Line quite a bit before I could find a good value, it's not really intuitive what Line represents but low numbers <10 seemed to be what I needed – PICyourBrain Feb 15 '10 at 15:05
  • 2
    It didn't work for me. It turns out that this code doesn't work until the Form is loaded. Before then, it causes the description area to be zero lines high. – Qwertie Sep 21 '10 at 20:44
  • 1
    It's been a long time since this question was first asked, but I'm still here to help. I call this function in Form.OnShown. Try it out, it should work in that case. –  Sep 22 '10 at 14:19
  • Actually, the `Controls` property is hidden by a shadow homonym (non browsable due to ad-hoc attributes) and thus still available, like any other `Control`-derived class. Currently the shadowing property is forwarding to the base implementation. Therefore you can safely replace the first two lines with a simple "`var collection = grid.Controls`". – Rubidium 37 May 31 '22 at 09:24
0

And here's Matthew Ferreira's solution in VB.Net. Thanks Matthew, works a treat!

    Imports System.Reflection

    Public Sub ResizeDescriptionArea(grid As PropertyGrid, lines As Integer)
        Try
            Dim info = grid.[GetType]().GetProperty("Controls")
            Dim collection = DirectCast(info.GetValue(grid, Nothing), Control.ControlCollection)

            For Each control As Object In collection
                Dim type = control.[GetType]()

                If "DocComment" = type.Name Then
                    Const Flags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic
                    Dim field = type.BaseType.GetField("userSized", Flags)
                    field.SetValue(control, True)

                    info = type.GetProperty("Lines")
                    info.SetValue(control, lines, Nothing)

                    grid.HelpVisible = True
                    Exit For
                End If
            Next

        Catch ex As Exception
            Trace.WriteLine(ex)
        End Try
    End Sub
Pauli Price
  • 4,187
  • 3
  • 34
  • 62
0

You cannot do that with the public methods and properties exposed by the PropertyGrid control, or at least I couldn't find anything useful.
You might try to use reflection to get the sub-controls of the property grid that display the settings or the description, and try to set their height programmatically; I guess that the splitter is just docked, and setting it position would not change anything.
Looking at the PropertyGrid's non-public members with the debugger should help you find out about the internal structure of the control.

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
0

I had to make some adjustments to make this work now. The following works for .NET 6:

public static class PropertyGridExtensions {
    public static void ResizeDescriptionArea(this PropertyGrid grid, int lineCount) {
        var helpControl = grid.Controls[0];
        var helpControlType = helpControl.GetType();

        const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
        helpControlType.GetProperty("Lines")?.SetValue(helpControl, lineCount);
        helpControlType.BaseType!.GetProperty("UserSized", bindingFlags)!.SetValue(helpControl, true);

        grid.HelpVisible = true;
    }
}
Robert Massa
  • 4,345
  • 1
  • 32
  • 44