Results 1 to 3 of 3

Thread: listbox additem

  1. #1

    Thread Starter
    Fanatic Member vishalmarya's Avatar
    Join Date
    Feb 2001
    Location
    New Delhi , INDIA
    Posts
    858

    listbox additem

    i want to add items in a listbox along with its corresponding id.

    my code :


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim obj As class1

    obj = New class1
    obj.name = "xyz"
    obj.id = 1

    ListBox1.Items.Add(obj)

    obj = New class1
    obj.name = "pqr"
    obj.id = 2

    ListBox1.Items.Add(obj)


    End Sub

    Private Class class1

    Public name As String
    Public id As Integer

    End Class

    result shown on the form is :


    windowsapplication1.form1+class1
    windowsapplication1.form1+class1

    Any suggestions.
    Vishal Marya, MCP .net 3.5
    My Site
    http://www.vstoolsgallery.com/
    http://visualstudiogallery.msdn.micr...b-f87a909b9266





    Please indicate what version of vb you use.
    Please mark your thread resolved using the Thread Tools above.
    -----------------------------------------

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: listbox additem

    You cannot do that with the WinForms ListBox, only with the WebForms version. If you want a hidden value associated with each displayed value then you have to create a data source and bind it to the ListBox. If you aren't aware of how to use them already, look up the DataSource, DisplayMember, ValueMember and SelectedValue properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    Re: listbox additem

    You'd be better off using the ListView control. You could stored hidden values in each ListViewItem's Tag property.

    http://msdn.microsoft.com/library/de...ClassTopic.asp
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

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