How to Create an User Defined Property for a Combo Box User Contro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How to Create an User Defined Property, which stores value for each index of
the Combo Box User Control.(each value is related to the item stored in the
ComboBox)
 
Hi,

Databind the combobox to an arraylist filled with a class that holds
the two values.

Dim alCombo As New ArrayList

For i As Integer = 0 To 10

Dim c As New MyComboboxData

With c

..MyReturnValue = "Value " & i.ToString

..show = i.ToString

End With

alCombo.Add(c)

Next

ComboBox1.DataSource = alCombo

ComboBox1.DisplayMember = "show"

ComboBox1.ValueMember = "MyReturnValue"



Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

Try

Dim c As MyComboboxData

c = CType(ComboBox1.SelectedItem, MyComboboxData)

Debug.WriteLine(c.MyReturnValue)

Catch ex As Exception

End Try

End Sub



The class



Public Class MyComboboxData

Dim mstrShow As String

Dim mstrValue As String

Public Property show() As String

Get

Return mstrShow

End Get

Set(ByVal Value As String)

mstrShow = Value

End Set

End Property

Public Property MyReturnValue() As String

Get

Return mstrValue

End Get

Set(ByVal Value As String)

mstrValue = Value

End Set

End Property

End Class



Ken
 
As an added shortcut, give your class a "ToString" function that overrides
the base ToString function and return the string you want to display. Then
you don't even have to set the .DisplayMember property.

-Zorpy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top