[RESOLVED] concatenating the content of a listbox
Hi
I have a listbox that contains filenames,the listbox can contain 1 filename or more(up to 10).I want the content of the listbox to be displayed in a string Msg as follow:
If the listbox contains File1,File2 and File3,I want the string Msg to be:
Msg="File1,File2,File3"
but if the listbox contains only one filename,let's Msg be:
Msg="File1"
I tried a code but I obtained a comma at the the end of the string like
Msg="File1,".So how can I avoid the last comma?
thanks
Re: concatenating the content of a listbox
VB Code:
msg = left(msg,len(msg)-1)
Re: concatenating the content of a listbox
I think the easiest way is to remove the last comma.
VB Code:
Private Sub Command1_Click()
Dim Msg As String
Dim i As Integer
For i = 0 To List1.ListCount - 1
Msg = Msg & List1.List(i) & ","
Next
If Len(Msg) > 0 Then
Msg = Left(Msg, Len(Msg) - 1)
End If
MsgBox Msg
End Sub
Re: concatenating the content of a listbox
Or you can use an array and the Join() function specifying a comma delimeter. It might be too much for now, but this approach helps when your dealing with string arrays from Split(), etc, so it might be useful to know in advance just in case.
VB Code:
Dim srrFileNames() As String
Dim i As Integer
ReDim srrFileNames(List1.ListCount - 1)
For i = 0 To List1.ListCount - 1
srrFileNames(i) = List1.List(i)
Next
Msg = Join(srrFileNames, ",")
Re: concatenating the content of a listbox
Hi
thanks to you all,Leinad31,Frans C and JMACP
it was what I wanted