Create a dropdown list of valid property values for a custom control

I've created a custom user control that has several properties. One specifies which database I want the control to access. I want to be able to present the user of the control a drop down from which he can select which database the control will interact with.

How do I get the dropdown to work? I can get default values, but have yet to figure out how to get the selectable list.

Any help is apprectiated.




You just need to attach your own TypeConverter to your property. You will override the GetStandardValuesSupported and GetStandardValues methods (maybe GetStandardValuesExclusive too) to return the list of databases you want to show.

If you are new to the PropertyGrid and the TypeConverter, here is a document.

It turns out to be simpler than I thought.

I had an enumeration set up for the property, but was having trouble using it for the property type. Said it was unaccessable outside of the class.

Then I had a 'duh' moment and changed the enumeration from Friend to Public, and then I was able to use the enumeration as the property type. As a result the values from the enumeration are listed in a dropdown when I look at the values for that property of the controls.

Thanks to all that answered.


I'm a little confused about your problem.

If your user control contains a DropDownList control, just inititalize somewhere in the user control.

The easiest way is in the Codebehind for the usercontrol, just do DropDownList.Items.Add() or whatever the syntax is for adding an item.

Here is the template I use to create a custom datasource for my comboboxes:

    Private Class Listing

    Private _List As New ArrayList

    Public Sub Add(ByVal ItemNumber As Integer, ByVal ItemName As String)

        _List.Add(New dataItem(ItemNumber, ItemName))

    End Sub

    Public ReadOnly Property List() As ArrayList
            Return _List
        End Get
    End Property

End Class

Private Class dataItem
    Private _ItemNumber As Integer
    Private _ItemName As String

    Public Sub New(ByVal intItemNumber As Integer, ByVal strItemName As String)
        Me._ItemNumber = intItemNumber
        Me._ItemName = strItemName
    End Sub

    Public ReadOnly Property ItemName() As String
            Return _ItemName
        End Get
    End Property

    Public ReadOnly Property ItemNumber() As Integer
            Return _ItemNumber
        End Get
    End Property

    Public ReadOnly Property DisplayValue() As String

            Return CStr(Me._ItemNumber).Trim & " - " & _ItemName.Trim
        End Get

    End Property

    Public Overrides Function ToString() As String

        Return CStr(Me._ItemNumber).Trim & " - " & _ItemName.Trim

    End Function

End Class

And this is how I load it:

    ListBindSource = New Listing

    For Each Row As DataRowView In Me.BindingSource.List
        Dim strName As String = String.Empty
        Dim intPos As Integer = Me.BindingSource.Find("Number", Row("Number"))
        If intPos > -1 Then
            Me.BindingSource.Position = intPos
            strName = Me.BindingSource.Current("Name")
        End If
        ListBindSource.Add(Row("Number"), strName)

    cboNumber.DataSource = ListBindSource.POList
    cboNumber.DisplayMember = "DisplayValue"
    cboNumber.ValueMember = "Number"
    AddHandler cboNumber.SelectedIndexChanged, AddressOf _

Hope this helps. One thing to keep in mind is that if cboNumber has a handler already assigned to the SelectedIndexchanged event, you will encounter problems. So don't create a default event.

Need Your Help

Understanding vars in Clojure

data-structures clojure immutability

I'm fairly new to Clojure and I'm having a difficult time understanding what's happening behind the scenes with the following code:

fix font size on a website globally

html css ckeditor

I m using CKEditor for a simple CMS i built for a client.