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?
Printable View
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?
Welcome On the forums :wave:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.ListBox1.Items.Add("Shakti") Me.ListBox1.Items.Add("Singh") Me.ListBox1.Items.Add("Dulawat") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Item As Object For Each Item In Me.ListBox1.Items Me.Label1.Text = Me.Label1.Text + Item Next 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.
Thanks for the guide :thumb:
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
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,Quote:
Originally Posted by CarraSome
How did you coded, because I've tryed and it's working like it should.
Without any errors.
Wkr,
sparrow1
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,Quote:
Originally Posted by CarraSome
You can try this:
VB Code:
Me.Listbox1.Items.Add(txtPet.Text) ' add item to listbox Dim Item As Object For Each Item In Me.ListBox1.Items Me.Label1.Text = Me.Label1.Text + Item Next
Hope it helps,
sparrow1
what does the Me. actually do?
I'm still having the same error...
has it got to do with the Me. ?
Hi,Quote:
Originally Posted by CarraSome
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Item As Object For Each Item In Me.ListBox1.Items Me.Label1.Text = Me.Label1.Text + Item Next End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.ListBox1.Items.Add(TextBox1.Text) End Sub
Wkr,
sparrow1
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.Quote:
Operator is not balid for string "" and type 'ListItem'
Exception Details: SYstem.InvalidCastException: Operator is nto balid for string "" and type ListItem ''
VB Code:
'displays each item in the listbox on a new line in a label For Each Item As Object In ListBox1.Items Label1.Text &= Item.ToString & Environment.NewLine Next