Codessentials

  • Increase font size
  • Default font size
  • Decrease font size
Home Coding tips .Net Load objects in a Combobox

Load objects in a Combobox

If you want to load objects in a combobox rather than just text strings, then here is how to:

Add a combobox to your form with the name cbBooks.

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Dim aBooks As List(Of Book) = New List(Of Book)
 
   aBooks.Add(New Book("The Iliad", "9780140275360"))
   aBooks.Add(New Book("The Odyssey", "9780679728139"))
   aBooks.Add(New Book("Oedipus the King", "9781416500339"))
   
   cbBooks.DataSource = aBooks
   cbBooks.ValueMember = "Isbn"
   cbBooks.DisplayMember = "Title"
End Sub

Private Class Book

   Private _Title As String
   Private _Isbn As Integer
 
   Public Property Title() As String
      Get
         Return Me._Title
      End Get
 
      Set(ByVal value As String)
         Me._Title = value
      End Set
   End Property
 
   Public Property Isbn() As Integer
      Get
         Return Me._Isbn
      End Get
 
      Set(ByVal value As Integer)
         Me._Isbn = value
      End Set
   End Property
 
   Public Sub New(ByVal aTitle As String, ByVal aIsbn As Integer)
      Me.Title = aTitle
      Me.Isbn = aIsbn
   End Sub
 
End Class
 

Bookmark

AddThis Social Bookmark Button

Related Articles


Newsflash

Yadis! Backup 1.9.8 is released!

See the new features right here!

Download this version here!