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 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.