Results 1 to 10 of 10

Thread: Simple listbox problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    8

    Simple listbox problem

    hi... I have just started VB.net and tried to find online for solutions to my problem but I couldn't understand......

    say if I have a list box called ListBox1

    how do I actually display all data from ListBox1 into a label?

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up Re: Simple listbox problem

    Welcome On the forums

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Me.ListBox1.Items.Add("Shakti")
    3.         Me.ListBox1.Items.Add("Singh")
    4.         Me.ListBox1.Items.Add("Dulawat")
    5.     End Sub
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         Dim Item As Object
    9.         For Each Item In Me.ListBox1.Items
    10.             Me.Label1.Text = Me.Label1.Text + Item
    11.         Next
    12.     End Sub

    This is the just simple example as you want but i suggest use MSDN for learning all the property and use of the DOT net event.
    For more detail click on the MSDN at my signature and search there
    .

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    8

    Re: Simple listbox problem

    Thanks for the guide

    for the second part of the code

    Dim Item As Object
    For Each Item In Me.ListBox1.Items
    Me.Label1.Text = Me.Label1.Text + Item
    Next

    is this the only way?

    What about

    Dim i as Integer
    For i =0 To ListBox1.Items.Count - 1
    .......
    Next

    I'm actually wondering what could be put inside the ... I tried
    label1.text = ListBox.Items(i)
    and a few different type all are not able to work

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    8

    Re: Simple listbox problem

    oh I tried your codings and I have some errors

    Operator is not balid for string "" and type 'ListItem'

    Exception Details: SYstem.InvalidCastException: Operator is nto balid for string "" and type ListItem ''

    inside the listbox I put numbers(int)

  5. #5
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Simple listbox problem

    Quote Originally Posted by CarraSome
    oh I tried your codings and I have some errors

    Operator is not balid for string "" and type 'ListItem'

    Exception Details: SYstem.InvalidCastException: Operator is nto balid for string "" and type ListItem ''

    inside the listbox I put numbers(int)
    Hi,

    How did you coded, because I've tryed and it's working like it should.
    Without any errors.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    8

    Re: Simple listbox problem

    This is the part of my codes

    for adding into the listbox

    Dim pet As String
    pet = txtPet.Text
    Listbox1.Items.Add(pet)

    for extracting its

    Dim Item As Object
    For Each Item In ListBox1.Items
    Label1.Text = Label1.Text + Item
    Next

  7. #7
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Simple listbox problem

    Quote Originally Posted by CarraSome
    This is the part of my codes

    for adding into the listbox

    Dim pet As String
    pet = txtPet.Text
    Listbox1.Items.Add(pet)

    for extracting its

    Dim Item As Object
    For Each Item In ListBox1.Items
    Label1.Text = Label1.Text + Item
    Next
    Hi,

    You can try this:


    VB Code:
    1. Me.Listbox1.Items.Add(txtPet.Text) ' add item to listbox
    2.  
    3. Dim Item As Object
    4.         For Each Item In Me.ListBox1.Items
    5.            Me.Label1.Text = Me.Label1.Text + Item
    6.         Next

    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    8

    Re: Simple listbox problem

    what does the Me. actually do?

    I'm still having the same error...

    has it got to do with the Me. ?

  9. #9
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Simple listbox problem

    Quote Originally Posted by CarraSome
    what does the Me. actually do?

    I'm still having the same error...

    has it got to do with the Me. ?
    Hi,

    You can try to create a new testapp with a listbox, label and textbox ans 2 buttons, then use this code. Write something into the textbox and added into the listbox you'll see that it works


    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim Item As Object
    3.         For Each Item In Me.ListBox1.Items
    4.             Me.Label1.Text = Me.Label1.Text + Item
    5.            
    6.         Next
    7.     End Sub
    8.  
    9.        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    10.         Me.ListBox1.Items.Add(TextBox1.Text)
    11.     End Sub

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Simple listbox problem

    Operator is not balid for string "" and type 'ListItem'

    Exception Details: SYstem.InvalidCastException: Operator is nto balid for string "" and type ListItem ''
    The errors are because you are using the "+" operator instead of the string concatenation operator, which you should be using. Also, you are trying to concatenate an object to a string, which you shouldn't do. You should use the "&" character to concatenate, and then use the object's .ToString method in order to get the string representation of the object in the listbox.

    VB Code:
    1. 'displays each item in the listbox on a new line in a label
    2. For Each Item As Object In ListBox1.Items
    3.        Label1.Text &= Item.ToString & Environment.NewLine
    4. Next

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