StringConverter Class

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
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?
 
(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.
 
Thanks, I'll look into that. I wonder why the poster appears to have been banned? I hope there's nothing malicious in the code.
 
JoeMamma wasn't banned because he posted malicious code (in fact, he posted plenty of good code), he was banned because he had a malicious mouth.
 
This is easy once you know how.

Define your property with the attribute as follows:

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

Then define your editor class:

Visual Basic:
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.
 
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.
 
Back
Top