Results 1 to 5 of 5

Thread: object and combobox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    object and combobox

    Hi,
    I've created my own object called customer, and have put them into a collection. How do i go about putting these in a combobox, displaying the name, but also having a key associated with each customer(already there called customerID)

    In vb6 i used to put the customerID in the itemData property of a combo box but this doesn't seem to be here for vb.net


    Many thanks
    nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Just use DataBinding to the collection. Use the collection as the datasource then set the DisplayMember to the fieldname that you want to be like the text, and the valuemember to the fieldname that you want to be the ItemData type.
    VB Code:
    1. ComboBox1.DataSource=CustomerCollection
    2. ComboBox1.DisplayMember="CustomerName"
    3. ComboBox1.ValueMember="CustomerID"

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    cboSupplier.valuemember = "personID"

    gives an error :

    cast from type 'Microsoft.visualbasic.collection' to type 'Object()' is invalid.

    If i comment that line out then

    cboSupplier.displayMember = "companyName"

    doesn't seem to work at all :S


    How does the combo box know what the object in the collection is?
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Are you inheriting or using the throw back to the old vb6 collection? In order for it to bind to the items property correctly then the collection must implement IList which all .NET collections do or at least most of the classes in the System.Collections namespace do. Otherwise I guess you can just loop through your collection and add each one as an item. You may have to override the ToString function to get it to display the right text.
    Last edited by Edneeis; Nov 25th, 2003 at 11:34 AM.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    that's it! I had it as a standard collection.
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width