如何以编程方式调整 PropertyGrid 控件的水平分隔线?

发布于 2024-08-20 23:29:40 字数 108 浏览 4 评论 0原文

我在我的 C# 项目中使用 .NET PropertyGrid 控件。

当加载包含网格的表单时,水平分隔符(将设置与描述分开)位于默认位置。如何在 C# 中以编程方式更改该拆分器的位置?

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#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烂人 2024-08-27 23:29:40

此代码基于一篇文章 (http://www.codeproject.com/KB/ grid/GridDescriptionHeight.aspx)来自代码项目,其中包含两个修复和一些清理工作。

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

我在自己的项目中使用过它;它应该适合你。

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.

羁〃客ぐ 2024-08-27 23:29:40

我现在必须做出一些调整才能使这项工作正常进行。以下适用于 .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;
    }
}

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;
    }
}
べ映画 2024-08-27 23:29:40

您无法使用 PropertyGrid 控件公开的公共方法和属性来做到这一点,或者至少我找不到任何有用的东西。
您可能会尝试使用反射来获取显示设置或说明的属性网格的子控件,并尝试以编程方式设置它们的高度;我猜分离器只是对接,设置它的位置不会改变任何东西。
使用调试器查看 PropertyGrid 的非公共成员应该可以帮助您了解控件的内部结构。

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.

半世蒼涼 2024-08-27 23:29:40

这是 Matthew Ferreira 在 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文