CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2009
    Posts
    5

    Exclamation help: looping through listbox items to split string

    hi experts!

    i need some help to develop looping logic as the following:

    when have a listbox say "list1" contains some lines of string as items.

    actually, i need to loop through these items line by line, and then separate each of them into words.
    my try was like this:

    -------------------------------------------------------------------
    Dim arrWords() As String
    Dim i, x As Integer

    Text1.Text = ""
    For i = 0 To List1.ListCount
    For x = 0 To 6
    arrWords(x) = Split(Str(List1.List(i)))
    If Text1.Text = "" Then
    Text1.Text = arrWords(x)
    Else
    Text1.Text = Text1.Text & vbCrLf & arrWords(x)
    End If
    Next x

    Next i
    ----------------------------------------------------------------------

    any help would be highlly appreciated.. thnx in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: help: looping through listbox items to split string

    Please use CODE TAGS around your code
    Code:
    ' Like this
    What is the error message you're getting?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2009
    Posts
    5

    Re: help: looping through listbox items to split string

    oh sorry, here is the code again:

    Code:
    Dim arrWords() As String
    Dim i, x As Integer
    
    Text1.Text = ""
    For i = 0 To List1.ListCount
     For x = 0 To List1.ListCount
         arrWords(x) = Split(Str(List1.List(i)))
         If Text1.Text = "" Then
             Text1.Text = arrWords(x)
         Else
             Text1.Text = Text1.Text & vbCrLf & arrWords(x)
         End If
     Next x
    Next i
    and the error massege is "Run-time error '13': Type mismatch"

    thanks

  4. #4
    Join Date
    Sep 2009
    Posts
    126

    Re: help: looping through listbox items to split string

    On which line do you get the error code?

  5. #5
    Join Date
    Dec 2009
    Posts
    5

    Re: help: looping through listbox items to split string

    on this line:
    Code:
    arrWords(x) = Split(Str(List1.List(i)))
    thanks

  6. #6
    Join Date
    Dec 2009
    Posts
    5

    Re: help: looping through listbox items to split string

    hi guys

    i am here again just to tell you that i got over the problem by using vb9, ".net" with code like the following structure:
    "it may helps someone else"

    Code:
            Dim i As Integer
            For i = 0 To ListBox1.Items.Count - 1
                Dim arrWords() As String = ListBox1.Items(i).Split(" ")
                For x = 0 To UBound(arrWords)
                    ListBox2.Items.Add(arrWords.ElementAt(x))
                Next x
            Next i
    so many thanks for all of you.

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: help: looping through listbox items to split string

    Maybe this is good for nothing at this point anymore, but:
    I'd like to point out that the very same could have been achieved in VB6, too.

    Code:
            Dim i As Integer
            Dim arrWords() as String
            For i = 0 To ListBox1.Items.Count - 1
                arrWords = Split(ListBox1.Items(i), " ")
                For x = 0 To UBound(arrWords)
                    ListBox2.AddItem arrWords(x)
                Next x
            Next i
    I think the previous type mismatch lied in this instruction: Split(Str(List1.List(i)))
    The list elements are already strings, so using the Str() function is obsolete and produces the error: it expects anumeric argument.

  8. #8
    Join Date
    Dec 2009
    Posts
    5

    Re: help: looping through listbox items to split string

    yup, you are totally right..
    it works Smoothly too
    thanks a lot.

  9. #9
    Join Date
    Apr 2018
    Posts
    2

    Red face Re: help: looping through listbox items to split string

    thank you so much !! it did help me!!

  10. #10
    Join Date
    Apr 2018
    Posts
    2

    Re: help: looping through listbox items to split string

    thank you sooo much man

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

Featured