Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

This article says to use the StringConverter to provide for a drop down list for a property in the property grid. Two questions:

 

1. Why does it need to be the StringConverter? The TypeConverter class seems to do the job equally well, and

 

2. Is it possible to make the drop down list multi-select?

  • Leaders
Posted

(1) I've seen tutorials that use TypeConverters, so I don't really know if there is any real advantage to using stringConverters.

 

(2) I'm nearly positive that the property grid doesn't come with this feature out of the box. You'll notice that no simple dropdowns in VS allow you to select multiple enum members. Those properties where you can select multiple enum members (for example, the anchor property of a control) use custom editors. It would be nice if there were built in support for something like an enum dropdown list with checkboxes.

[sIGPIC]e[/sIGPIC]
Posted

This is easy once you know how.

 

Define your property with the attribute as follows:

 

<Editor(GetType(CustomEditor)> Public Property MyStringProp as String...

 

Then define your editor class:

 

Public Class CustomEditor
Inherits System.Drawing.Design.UITypeEditor


Public Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.Drawing.Design.UITypeEditorEditStyle
       Return Drawing.Design.UITypeEditorEditStyle.DropDown
End Function

Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object

       Dim ui As New System.Windows.Forms.CheckedListBox
       ui.Items.Add("ABC")
       ui.Items.Add("DEF")
       ui.Items.Add("GHI")
       ui.CheckOnClick = True
       ui.BorderStyle = Windows.Forms.BorderStyle.None

       Dim I As MyClass = context.Instance

       If I.MyStringProp <> "" Then
           Dim f() As String = I.MyStringProp.Split(",")
           For Each s As String In f
               If ui.Items.IndexOf(s) <> -1 Then ui.SetItemChecked(ui.Items.IndexOf(s), True)
           Next
       End If

       Dim fr As System.Windows.Forms.Design.IWindowsFormsEditorService = provider.GetService(GetType(System.Windows.Forms.Design.IWindowsFormsEditorService))
       fr.DropDownControl(ui)

       Dim s2 As String
       For Each s3 As String In ui.CheckedItems
           s2 = s2 & s3 & ","
       Next

       If Right$(s2, 1) = "," Then s2 = Left$(s2, Len(s2) - 1)

       ui.Dispose()

       Return MyBase.EditValue(context, provider, s2)
End Function

End Class

 

Obviously this code can be tidied up a little. Query whether I'm getting hold of the SelectedObject in the right way in using context.Instance. value always seems to be Nothing so I can't use that.

  • 1 month later...
Posted
If anyone does investigate this further, I'd love to know how you control whether the custom drop down extends all the way across the values column. Currently it always extends at least this far, but that's not very good where the dropdown is a non-resizeable control like the monthview. You get a lot of unnecessary white space surrounding the control when there's more room than is necessary.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...