For Visual Basic .NET Users (vb.net, Visual Studio 2022, March 2024): The internal control name of the description area changed from "DocComment" to "HelpPane" and it's property "UserSized" was renamed to "<UserSized>k__BackingField". To be compatible with all previous versions I rewrote the function as follows:
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 type.Name = "HelpPane" Or type.Name = "DocComment" Then
Const Flags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic
Dim fields = type.BaseType.GetFields(Flags)
' Method 1:
For Each fi As FieldInfo In fields
If fi.Name.Contains("UserSized") = True Then
Dim field = type.BaseType.GetField(fi.Name, Flags)
field.SetValue(control, True)
info = type.GetProperty("Lines")
info.SetValue(control, lines, Nothing)
grid.HelpVisible = True
Exit For
End If
Next
' or Method 2: (simpler)
' Dim field = type.BaseType.GetField("<UserSized>k__BackingField", 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
Debug.WriteLine(ex)
End Try
End Sub
Here is a method that doesn't rely on directly using private methods or reflection. It does still use undocumented interfaces though.
In .NET 4.0 the `PropertyGrid.Controls` collection contains 4 controls. `PropertyGrid.Controls.item(2)` is an undocumented `PropertyGridView` (same type as in answer that uses reflection). The property `PropertyGridView.LabelRatio` adjusts the relative widths of the columns. The range of `LabelRatio` looks like it is 1.1 to 9. Smaller values make the left column wider.
I know that setting `LabelRatio` before you initially display the control works. However I'm not sure what all you need to do to make it take effect once the control is already displayed. You can google MoveSplitterTo to find .NET source code and look at the source for `PropertyGridView` to get more details. The calculations and operations involving it seem somewhat complicated and I didn't analyze them in detail.
LabelRatio is initially set to 2 (ie splits the available PropertyGrid width in half). Set it to 3 for thirds, 4 for quarter.
Code requires Imports System.Reflection
Public Sub MoveVerticalSplitter(grid As PropertyGrid, Fraction 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 "PropertyGridView" = type.Name Then
control.LabelRatio = Fraction
grid.HelpVisible = True
Exit For
End If
Next
Catch ex As Exception
Trace.WriteLine(ex)
End Try
End Sub
To change size of Description Pane at bottom of PropertyGrid as lines of text
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