views:

529

answers:

4

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.

Thanks.

Marshall

A: 

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.

scottschulthess
I'm not talking about using a combobox in the user control itself. I'm talking about using it in the property box for the control. I have a property there for which I want to restrict the valid values. I've seen drop downs before in the property page and am trying to duplicate that behavior.
Marshall
+2  A: 

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.

Nicolas Cadilhac
A: 

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.

Marshall

Marshall
It would be very interresting to me to know how to display recommended value in the combobox. In my case I would like to extract them from a database.
controlbreak
If i want some dynamic values then what should i do? Values came from some class file, but i can't make it enumeration of it.
Krunal
A: 

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
        Get
            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
        Get
            Return _ItemName
        End Get
    End Property

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

    Public ReadOnly Property DisplayValue() As String

        Get
            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

    Me.BindingSource.MoveFirst()
    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)
    Next

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

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.

Marshall