Display a check-box next to a Boolean value in the property grid.
This is a bit of a cheat because it is unfortunately not possible to host a control in the value area of the PropertyGrid. It is however possible to draw in that area if we have the correct UITypeEditor.
UITypeEditor has a couple of methods GetPaintValueSupported and PaintValue that enable you to draw a preview of the value. This is how the thumbnail of images is displayed next to image data in the property grid. By creating an editor and assigning it to your boolean value you can display a checkbox which shows a check-mark whenever the value is set to true.
The following listing illustrates this technique.
using System;
using System.Drawing;
using System.Drawing.Design;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CheckBoxInPropertyGrid
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
this.propertyGrid1.SelectedObject=new CheckBoxInPropertyGrid();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(48, 24);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(200, 200);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.propertyGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
public class CheckBoxInPropertyGrid
{
bool _cb;
[Editor(typeof(CheckBoxInPropertyGridEditor),typeof(System.Drawing.Design.UITypeEditor))]
public bool Cb
{
get{return _cb;}
set{_cb=value;}
}
}
public class CheckBoxInPropertyGridEditor : UITypeEditor
{
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}
public override void PaintValue(PaintValueEventArgs e)
{
ControlPaint.DrawCheckBox(e.Graphics,e.Bounds,((CheckBoxInPropertyGrid)e.Context.Instance).Cb ? ButtonState.Checked : ButtonState.Normal);
}
}
}
For the VB users here is the same code in Visual Basic
Imports System
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Namespace CheckBoxInPropertyGrid
'/ <summary>
'/ Summary description for Form1.
'/ </summary>
Public Class Form1
Inherits System.Windows.Forms.Form
Private propertyGrid1 As System.Windows.Forms.PropertyGrid
'/ <summary>
'/ Required designer variable.
'/ </summary>
Private components As System.ComponentModel.Container = Nothing
Public Sub New()
'
' Required for Windows Form Designer support
'
InitializeComponent()
'
' TODO: Add any constructor code after InitializeComponent call
'
Me.propertyGrid1.SelectedObject = New CheckBoxInPropertyGrid()
End Sub 'New
'/ <summary>
'/ Clean up any resources being used.
'/ </summary>
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Dispose
#Region "Windows Form Designer generated code"
'/ <summary>
'/ Required method for Designer support - do not modify
'/ the contents of this method with the code editor.
'/ </summary>
Private Sub InitializeComponent()
Me.propertyGrid1 = New System.Windows.Forms.PropertyGrid
Me.SuspendLayout()
'
' propertyGrid1
'
Me.propertyGrid1.CommandsVisibleIfAvailable = True
Me.propertyGrid1.LargeButtons = False
Me.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar
Me.propertyGrid1.Location = New System.Drawing.Point(48, 24)
Me.propertyGrid1.Name = "propertyGrid1"
Me.propertyGrid1.Size = New System.Drawing.Size(200, 200)
Me.propertyGrid1.TabIndex = 0
Me.propertyGrid1.Text = "propertyGrid1"
Me.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window
Me.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText
'
' Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(propertyGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub 'InitializeComponent
#End Region
'/ <summary>
'/ The main entry point for the application.
'/ </summary>
<STAThread()> _
Shared Sub Main()
Application.Run(New Form1)
End Sub 'Main
End Class 'Form1
Public Class CheckBoxInPropertyGrid
Private _cb As Boolean
<Editor(GetType(CheckBoxInPropertyGridEditor), GetType(System.Drawing.Design.UITypeEditor))> _
Public Property Cb() As Boolean
Get
Return _cb
End Get
Set
_cb = value
End Set
End Property
End Class 'CheckBoxInPropertyGrid
Public Class CheckBoxInPropertyGridEditor
Inherits UITypeEditor
Public Overloads Overrides Function GetPaintValueSupported(ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function 'GetPaintValueSupported
Public Overloads Overrides Sub PaintValue(ByVal e As PaintValueEventArgs)
ControlPaint.DrawCheckBox(e.Graphics, e.Bounds, IIf(CType(e.Context.Instance, CheckBoxInPropertyGrid).Cb, ButtonState.Checked, ButtonState.Normal))
End Sub 'PaintValue
End Class 'CheckBoxInPropertyGridEditor
End Namespace 'CheckBoxInPropertyGrid
Return to the Windows Forms Tips and Tricks page |