Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox.Items to String

Status
Not open for further replies.

Frihi

Programmer
Jun 24, 2001
48
0
0
CH
How can I extract all the items (text) of a listbox and put them all into one string into a textbox as a file (Tom,Jim,Jack,...)

Something like:

Dim item As String
For Each item In ListBox1.Items
Me.List1.Text = Me.List1.Text + item
Next
MsgBox(List1.Text)

But I don't know how to convert DataRowView into String.

 
Hello Frihi.

Not sure if I understand your question properly, but this might help you. In the following example you need a listbox named ListBox1, and a button named Button1 on your form. Add a few items to the listbox before you start.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim objItem As Object
        Dim strAllItems As String

        For Each objItem In ListBox1.Items
            strAllItems &= CStr(objItem)
        Next

        MsgBox(strAllItems)

End Sub

If you had the items "John", "Sarah", and "Pete" in your listbox this would display a msgbox with "JohnSarahPete" as the message. Let me know if it's not what you want.

T0AD

There's a thin line between genius, and insanity!
 
Thankyou. Well this works fine if I do it with a handmade Listbox as you say. But when I try it with my own Listbox, that is filled out of a Datatable (Access.mdb) I still get the same message:

Invalid convertion of DataRowView into String...

And, btw, how can I separate the values with a semicolon:

"John,Sarah,Pete"

Thanks for your efforts

Fritz
 
Ah, don't know much about databases at the moment, learning now ;) sorry.

To add the comma (,) you could amend my code as follows:

Code:
For Each objItem In ListBox1.Items
    strAllItems &= CStr(objItem) & ","
Next

'To remove the last comma.
strAllItems = strItems.SubString(0, strItems.Length-1)

Maybe someone else can help with the 1st part.

There's a thin line between genius, and insanity!
 
Try This :

You have to remember that the items collection holds objects.

Dim objItem As Object
Dim strAllItems As String

For Each objItem In ListBox1.Items
strAllItems &= CStr(CType(objItem,DataRowView).ToString ) & ";"
Next

MsgBox(strAllItems)
 
Thank you once more for your tips. Well, this now gives me:

"System.Data.DataRowView,System.Data.DataRowView,System.Data.DataRowView,..."

Meanwhile I have been looking for another way. Instead of the ListBox I'm using now the DataSource itself to extract the String. I have a Label1 bound to the DataTable and the String appears in Label2:

Dim i, x As Integer
x = Me.objDataSet1.Tables("DataTable").Rows.Count - 1

For i = 0 To x

Me.Label2.Text &= "," & Label1.Text

Me.BindingContext(objDataSet1, "DataTable").Position = (Me.BindingContext(objDataSet1, "DataTable").Position + 1)

Next i

And this does exactly what I need:

"element1,element2,element3,..."

Maybe this might be useful for somebody.

Thanks and greetings from Switzerland

Fritz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top