This article shows you how to implement custom controls in your PropertyGrid that can handle any property object type…
This was one of those where I kept revisiting it time after time to little avail. Hours of searching and article reading led to many dead ends and frustrated hours, until I found the answer. Like most coding nightmares, it was 5 simple lines put in exactly the right place! These lines:
1 2 3 4 5 |
Public Overrides ReadOnly Property IsDropDownResizable As Boolean Get Return True End Get End Property |
Step 1: Create a new UITypeEditor
This interfaces the property to the custom editor. value contains the value passed from the property. It can be of any object type. You can therefore pass this to your custom control to inform any editing functions therein.
Create a new class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
Imports System.ComponentModel Imports System.Drawing.Design Imports System.Windows.Forms.Design Public Class TestUIEditor Inherits UITypeEditor ' Indicate that we display a dropdown. Public Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) _ As UITypeEditorEditStyle Return UITypeEditorEditStyle.DropDown End Function Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object ' Get an IWindowsFormsEditorService object. Dim editor_service As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService) If editor_service Is Nothing Then Return MyBase.EditValue(context, provider, value) End If 'Setup the editor Dim editorService As IWindowsFormsEditorService = Nothing If provider IsNot Nothing Then editorService = TryCast(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService) End If If editorService IsNot Nothing Then Dim testUI As New TestPropGridUI(editorService) 'Populate the existing value to the dropdown control testUI.Text = value 'Drop down the control editorService.DropDownControl(testUI) 'Update property value once dropdown closed value = testUI.Text End If Return value End Function Public Overrides ReadOnly Property IsDropDownResizable As Boolean Get 'Ensures control is resizable Return True End Get End Property End Class |
Step 2: Create your custom editor:
In this example, I have just used a custom class that inherits TextBox . However, you can use any controls, even a custom UserControl .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Imports System.Windows.Forms.Design Public Class TestPropGridUI Inherits TextBox ' The editor service displaying this control. Private m_EditorService As IWindowsFormsEditorService Public Sub New(ByVal editor_service As IWindowsFormsEditorService) MyBase.New() m_EditorService = editor_service Dock = DockStyle.Fill End Sub Public Sub returnPressed(sender As Object, e As KeyEventArgs) Handles Me.KeyDown 'Closes the dropdown when Enter pressed If e.KeyCode = Keys.Enter Then m_EditorService.CloseDropDown() End If End Sub End Class |
Note m_EditorService.CloseDropDown . Put this wherever you want the dropdown to close itself (e.g. after a value selection).
Step 3: Tell your class property to use the custom editor:
1 2 3 4 5 6 7 8 9 |
Public Class TestObject Public Property ID as String Public Property Quantity as Integer <Editor(GetType(TestUIEditor), GetType(UITypeEditor))> Public Property TestText As String End Class |
Leave a Reply