How to programmatically adjust the horizontal separator of a PropertyGrid control?

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

When the form containing the load on the grid, the horizontal divider (which separates the settings from the description) is in the default position. How to change the position of this splitter programmatically in C #?

+3
source share
3 answers

This code is based on an article ( http://www.codeproject.com/KB/grid/GridDescriptionHeight.aspx ) from the Code project, with two fixes and some cleanup.

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

; .

+7

, PropertyGrid, , , . , , , ; , , .
PropertyGrid .

0

VB.Net. , !

    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
0
source

Source: https://habr.com/ru/post/1732436/


All Articles