Results 1 to 5 of 5

Thread: [RESOLVED] concatenating the content of a listbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Resolved [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

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: concatenating the content of a listbox

    VB Code:
    1. msg = left(msg,len(msg)-1)

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: concatenating the content of a listbox

    I think the easiest way is to remove the last comma.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim Msg As String
    3. Dim i As Integer
    4.     For i = 0 To List1.ListCount - 1
    5.         Msg = Msg & List1.List(i) & ","
    6.     Next
    7.     If Len(Msg) > 0 Then
    8.         Msg = Left(Msg, Len(Msg) - 1)
    9.     End If
    10.     MsgBox Msg
    11. End Sub
    Frans

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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:
    1. Dim srrFileNames() As String
    2. Dim i As Integer
    3.  
    4.    ReDim srrFileNames(List1.ListCount - 1)
    5.    For i = 0 To List1.ListCount - 1
    6.       srrFileNames(i) = List1.List(i)
    7.    Next
    8.    Msg = Join(srrFileNames, ",")

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: concatenating the content of a listbox

    Hi
    thanks to you all,Leinad31,Frans C and JMACP
    it was what I wanted

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