Results 1 to 8 of 8

Thread: text box question

  1. #1
    pixelperfect
    Guest

    text box question

    I have a multi-line text box that is updated automatically. Sometimes it could have 5 lines - sometimes 20 lines.

    I have found code somewhere to count the lines in the box however I need to do something with each line.

    I want to setup a loop to grab each line and do something.


    Any ideas?

    Thanks,
    pp

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Try using split(text1,vbcrlf)

    Then loop through each element iof the array. Just an idea.

  3. #3
    pixelperfect
    Guest
    is split a vb6 feature? It doesn't work on vb5. Any ideas?

    pp

  4. #4
    Hyperactive Member zer0_flaw's Avatar
    Join Date
    Apr 2001
    Posts
    448
    I don't think the "split" function is new just to VB6... I think you can use it in VB5 too but I have no way to test to be sure. Did you make sure you replaced "text1" with your textbox's name? Later,

    -zer0 flaw

  5. #5
    Megatron
    Guest
    Here is Microsoft's Split function (which they rewrote for VB5 and 4)
    VB Code:
    1. Public Function Split(ByVal sIn As String, Optional sDelim As _
    2.             String, Optional nLimit As Long = -1, Optional bCompare As _
    3.              VbCompareMethod = vbBinaryCompare) As Variant
    4.           Dim sRead As String, sOut() As String, nC As Integer
    5.           If sDelim = "" Then
    6.               Split = sIn
    7.           End If
    8.           sRead = ReadUntil(sIn, sDelim, bCompare)
    9.           Do
    10.               ReDim Preserve sOut(nC)
    11.               sOut(nC) = sRead
    12.               nC = nC + 1
    13.               If nLimit <> -1 And nC >= nLimit Then Exit Do
    14.               sRead = ReadUntil(sIn, sDelim)
    15.           Loop While sRead <> ""
    16.           ReDim Preserve sOut(nC)
    17.           sOut(nC) = sIn
    18.           Split = sOut
    19.       End Function
    20.  
    21.       Public Function ReadUntil(ByRef sIn As String, _
    22.             sDelim As String, Optional bCompare As VbCompareMethod _
    23.           = vbBinaryCompare) As String
    24.           Dim nPos As String
    25.           nPos = InStr(1, sIn, sDelim, bCompare)
    26.           If nPos > 0 Then
    27.               ReadUntil = Left(sIn, nPos - 1)
    28.               sIn = Mid(sIn, nPos + Len(sDelim))
    29.           End If
    30.       End Function

  6. #6
    pixelperfect
    Guest
    thank you very much - that should work.

    However, I'm trying to grab each line in a variable and processing it but I get an Type Mismatch error on the debug line.


    oneline = Split(txtCellContents, vbCrLf)

    Debug.Print oneline

    Now what?

    pp

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    'VB5 Split Function
    'split function for VB5 and under vb6 has the function
    'posted originally by Aaron Young

    Public Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
    Dim sParts() As String
    Dim lParts As Long
    Dim lPos As Long

    lPos = InStr(sString, sSeparator)
    While lPos
    ReDim Preserve sParts(lParts)
    sParts(lParts) = Left(sString, lPos - 1)
    sString = Mid(sString, lPos + Len(sSeparator))
    lPos = InStr(sString, sSeparator)
    lParts = lParts + 1
    Wend
    If Len(sString) Then
    ReDim Preserve sParts(lParts)
    sParts(lParts) = sString
    End If
    Split2 = IIf(lParts, sParts, Array())
    End Function

    'Example:
    'the individual lines go into an array
    'so you can access them as you would any array
    'for i = 0 to ubound(vLines)
    ' printer.print vlines(i)
    'next i
    '
    'or whatever

    Private Sub Form_Load()
    Dim vLines As Variant 'this will be your array
    Dim lLine As Long
    'put some useless text in the textbox
    Text1 = "wayne Is here"
    Text1 = Text1 & vbCrLf & "the ice man cometh"
    Text1 = Text1 & vbCrLf & "help me out will you"

    vLines = Split2(Text1.Text, vbCrLf)
    For lLine = 0 To UBound(vLines)
    Debug.Print vLines(lLine)
    Next
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    Yeah it's me.

    Here's how I would do it.

    So you have the split function working and you're pumped up to kick some app as$.
    Code:
    dim oneline() as string
    oneline = split(strString, vbcrlf)
    
    dim i as integer
    dim x as integer
    
    i = ubound(oneline)
    
    if i > 20 then
         for x = (i -20) to ubound(oneline)
                   'do what you have to do here
                   lstBox.additem oneline(x)
        next x
    end if
    Last edited by Cabral; Jun 15th, 2001 at 10:51 PM.

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