|
-
Oct 29th, 2006, 04:32 AM
#1
Thread Starter
New Member
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?
-
Oct 29th, 2006, 04:51 AM
#2
Re: Simple listbox problem
Welcome On the forums
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.
-
Oct 29th, 2006, 05:04 AM
#3
Thread Starter
New Member
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
-
Oct 29th, 2006, 05:13 AM
#4
Thread Starter
New Member
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)
-
Oct 29th, 2006, 05:47 AM
#5
Re: Simple listbox problem
 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
-
Oct 29th, 2006, 05:51 AM
#6
Thread Starter
New Member
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
-
Oct 29th, 2006, 06:05 AM
#7
Re: Simple listbox problem
 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:
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
-
Oct 29th, 2006, 06:22 AM
#8
Thread Starter
New Member
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. ?
-
Oct 29th, 2006, 06:28 AM
#9
Re: Simple listbox problem
 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:
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
-
Oct 29th, 2006, 10:33 AM
#10
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:
'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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|