How to add object to a combobox

To add the object as an item to a combobox is pretty easy, it is the same as you are adding a string/numeric value.

Example:

  1. Public class X
  2. Public Name as String
  3. Public Value as String
  4.  
  5. Public Sub New(StrName As String, StrValue As String)
  6. Name = StrName
  7. Value = StrValue
  8. End Sub
  9. End Class
  10.  
  11. To fill the combobox with the created object, let's say you have a combo box called cboX
  12. dim Obj as X
  13. For i as Integer = 0 to 10
  14.            cboX.Items.Add(New X("Name " & i.ToString(), "Value " & i.ToString())
  15. Next

Then you need to set the DisplayMember property of the Combo box so that the Text displayed in the
combobox is not “namespace.X”.

  1. cboX.DisplayMember = "Name"

,

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth