How to change the border color (or remove border) of a PropertyGrid control?

Having a standard WinForms 2.0 PropertyGrid control I'm looking for a way to change the border color of a control or remove the border altogether.

enter image description here

I know the LineColor property , which, unfortunately, only changes the internal borders between cells.

In addition, I used ILSpy to look at the source code of the PropertyGrid control and did not find anything meaningful to me.

My question is:

How to remove the outer border of a PropertyGrid control or change the color of the outer border?

Update 2012-05-04 - Solution (otherwise "hack"):

Based on Jamie's answer, I put together a working solution ( which you can download here ):

enter image description here

The idea is to place the property grid inside the panel and let the panel dock the control.

With this approach, I placed the trim panel in another panel with Padding “1” (or whatever you want the borders to be) and gave this BackColor panel, which serves as the border color (green in my example).

Set the property grid snap to Left, Right, Top, Down), set the Dock trim panel to Full.

enter image description here

This works well for my requirements. I would see it as a hack, since it consumes the resources of two panels, which I hoped I could save.

+7
source share
5 answers

This is another alternative, as it seems that my first answer is not suitable for this particular control. This is a dirty trick, but it should work:

Place the control panel in your window or dialog, say, with a size of 100H x 300V. Place the grid property inside the panel with a position of -1, -1 and a size of 102.302.

+2
source

Here is the code from my project

PropertyGrid has two controls that need a process.
+ doccomment is help with documents.
+ gridView displaying the value of the display property.

These controls draw a rectangle with the color ControlDark.

We need to draw the rectangle again with HelpBackColor and LineColor to make a clear presentation.

  namespace Bravo.Bravo7.UI { public class MyPropertyGrid : PropertyGrid { public class SnappableControl : NativeWindow { private Control _parent; private MyPropertyGrid _ownerGrid; public SnappableControl(Control parent, MyPropertyGrid ownerGrid) { _parent = parent; _parent.HandleCreated += _parent_HandleCreated; _parent.HandleDestroyed += _owner_HandleDestroyed; _ownerGrid = ownerGrid; } protected override void WndProc(ref Message m) { base.WndProc(ref m); switch (m.Msg) { case (int)NativeMethods.WM_NCPAINT: case (int)NativeMethods.WM_PAINT: using (var g = _parent.CreateGraphics()) { using (var pen = new Pen(_ownerGrid.HelpBackColor)) { var clientRectangle = _parent.ClientRectangle; clientRectangle.Width--; clientRectangle.Height--; g.DrawRectangle(pen, clientRectangle); } } break; } } void _owner_HandleDestroyed(object sender, EventArgs e) { ReleaseHandle(); } void _parent_HandleCreated(object sender, EventArgs e) { AssignHandle(_parent.Handle); } } public class PropertyGridView : NativeWindow { private Control _parent; private MyPropertyGrid _ownerGrid; public PropertyGridView(Control parent, MyPropertyGrid ownerGrid) { _parent = parent; _parent.HandleCreated += _owner_HandleCreated; _parent.HandleDestroyed += _owner_HandleDestroyed; _ownerGrid = ownerGrid; } protected override void WndProc(ref Message m) { base.WndProc(ref m); switch (m.Msg) { case (int)NativeMethods.WM_NCPAINT: case (int)NativeMethods.WM_PAINT: using (var g = _parent.CreateGraphics()) { using (var pen = new Pen(_ownerGrid.LineColor)) { g.DrawRectangle(pen, 0, 0, _parent.Width - 1, _parent.Height - 1); } } break; } } void _owner_HandleDestroyed(object sender, EventArgs e) { ReleaseHandle(); } void _owner_HandleCreated(object sender, EventArgs e) { AssignHandle(_parent.Handle); } } public class MyToolStripRenderer : ToolStripSystemRenderer { protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e) { //base.OnRenderToolStripBorder(e); } } public MyPropertyGrid() { base.LineColor = SystemColors.Control; base.ViewBackColor = Color.FromArgb(246, 246, 246); base.DrawFlatToolbar = true; base.ToolStripRenderer = new MyToolStripRenderer(); var docDocument = typeof(PropertyGrid) .GetField("doccomment", BindingFlags.NonPublic | BindingFlags.Instance) .GetValue(this) as Control; new SnappableControl(docDocument, this); var gridView = typeof(PropertyGrid) .GetField("gridView", BindingFlags.NonPublic | BindingFlags.Instance) .GetValue(this) as Control; new PropertyGridView(gridView, this); } } } 

Screen shot

+2
source

for this you need a little interaction:

 [DllImport("User32", CharSet=CharSet.Auto)] private static extern int SetWindowLong(IntPtr hWnd, int Index, int Value); [DllImport("User32", CharSet=CharSet.Auto)] private static extern int GetWindowLong(IntPtr hWnd, int Index); int GWL_STYLE = -16; int WS_BORDER = 0x00800000; IntPtr hWnd = yourPropertyGrid.Handle; int style = GetWindowLong(hWnd, GWL_STYLE); style = style & ~WS_BORDER; SetWindowLong(hWnd, GWL_STYLE, style); 
+1
source

this code works.

 private void SetHelpBoderColor(bool showBorder) { if (showBorder) { //Set Default ViewBackColor PropertyInfo viewBackColor = this.propertyGrid.GetType().GetProperty("ViewBorderColor"); if (viewBackColor != null) viewBackColor.SetValue(this.propertyGrid, SystemColors.ControlDark, null); //Set Default HelpBorderColor PropertyInfo helpBorderColor = this.propertyGrid.GetType().GetProperty("HelpBorderColor"); if (helpBorderColor != null) helpBorderColor.SetValue(this.propertyGrid, SystemColors.ControlDark, null); } else { //Set ViewBackColor PropertyInfo viewBackColor = this.propertyGrid.GetType().GetProperty("ViewBorderColor"); if (viewBackColor != null) viewBackColor.SetValue(this.propertyGrid, SystemColors.Control, null); //Set HelpBorderColor PropertyInfo helpBorderColor = this.propertyGrid.GetType().GetProperty("HelpBorderColor"); if (helpBorderColor != null) helpBorderColor.SetValue(this.propertyGrid, SystemColors.Control, null); } if (DesignMode) { Parent.Refresh(); } } 
0
source

One quick and intuitive solution is to create a parent panel and a child panel, as shown in the following figure:

enter image description here

0
source

All Articles