Skip to content

Instantly share code, notes, and snippets.

@MatthewKing
Created June 13, 2013 10:38
Show Gist options
  • Save MatthewKing/5772790 to your computer and use it in GitHub Desktop.
Save MatthewKing/5772790 to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
using System.Windows.Forms;
/// <summary>
/// Extension methods for System.Windows.Forms.PropertyGrid.
/// </summary>
public static class PropertyGridExtensions
{
/// <summary>
/// Move the PropetyGrid's splitter to the specified position.
/// </summary>
/// <param name="propertyGrid">The PropertyGrid that will have its splitter moved.</param>
/// <param name="position">The new position of the splitter.</param>
public static void MoveSplitterTo(this PropertyGrid propertyGrid, int position)
{
// This is a bit of a hack... There is an internal class known as
// System.Windows.Forms.PropertyGridInternal.PropertyGridView, which
// contains the 'MoveSplitterTo' method. We'll use reflection to call
// this MoveSplitterTo method!
// The System.Windows.Forms.PropertyGridInternal.PropertyGridView is
// always the 3rd Control in the PropertyGrid's Control collection.
Control propertyGridView = propertyGrid.Controls[2];
Type propertyGridViewType = propertyGridView.GetType();
// The method we want to call is defined as: private void MoveSplitterTo(int xpos)
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
MethodInfo methodInfo = propertyGridViewType.GetMethod("MoveSplitterTo", flags);
if (methodInfo != null)
methodInfo.Invoke(propertyGridView, new object[] { position });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment