raheel88 0 Newbie Poster

Hi all,

How can I make the items in a standard values collection identical to the items contained in a previously defined (dynamic) array of strings?

Here's a snippet;

Public Class ElementList : Inherits StringConverter

        Public Overloads Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function

        Public Overloads Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As StandardValuesCollection

            Return New StandardValuesCollection(New String() {elList})
        End Function

        Public Overloads Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function
    End Class

Here, elList is my shared array of strings, which has contents depending on the items in a listbox elsewhere on my form. So I want the items in the standard values collection to be equal to this listbox. The code doesn't work as it is, I assume I need to define some sort of delimiter in line 9?
The purpose of this is for a custom property grid which has a dropdown listbox as one of the properties. Thanks in advance!

Dani AI

Generated

— the fix is usually simpler than adding a delimiter: pass the string collection itself to the StandardValuesCollection constructor instead of wrapping it in a new single-element array. StandardValuesCollection accepts an array or any ICollection, so give it the actual collection type you have.

Example patterns (VB):

' elList is a String()
Return New StandardValuesCollection(elList)
' elList is List(Of String)
Return New StandardValuesCollection(elList)   ' List implements ICollection
' or explicitly
Return New StandardValuesCollection(elList.ToArray())

If you actually have one comma-delimited String, split it first:

Dim items = elList.Split(","c)
Return New StandardValuesCollection(items)

If the allowed values depend on the specific object being edited, use the provided context to read the instance instead of a Shared field so the list is per-instance:

Dim owner = TryCast(context.Instance, MyOwnerType)
If owner IsNot Nothing Then
    Return New StandardValuesCollection(owner.YourStringArray)
End If

Notes and troubleshooting:

  • If the dropdown does not reflect changes made at runtime, force the grid to refresh after updating the source: call TypeDescriptor.Refresh(yourObject) or propertyGrid.Refresh().
  • GetStandardValuesExclusive = True forces selection from the list only; set to False to allow free text.
  • Make sure the converter is applied to the property (TypeConverterAttribute) so your converter is used.

These adjustments should produce a proper list in the PropertyGrid and avoid the single-element-array issue caused by wrapping the array variable in a new initializer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.