none
Detect when word wrap has created a new line. RRS feed

  • Question

  • I have a Multiline textbox (textbox5) with word wrap = true

    As part of a script I need to mark a newlines with a "$" which is ok if one has been entered manually, but not if this is a result of word wrap. My current method is searching through char at a time and looking to see if the curline has changed but this is placing random spaces and line returns.

    Private Sub CommandButton9_Click() TextBox5.SetFocus linecount = 0 For z = 0 To Len(TextBox5.Text) - 1 TextBox5.SelStart = z If TextBox5.CurLine > linecount Then 'check to see if line has changed AR(ListBox1.ListIndex) = AR(ListBox1.ListIndex) & "$" & Mid(TextBox5.Text, z + 1, 1) MsgBox AR(ListBox1.ListIndex) linecount = linecount + 1 Else AR(ListBox1.ListIndex) = AR(ListBox1.ListIndex) & Mid(TextBox5.Text, z + 1, 1) End If Next 'Cut manual new lines AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbNewLine, "") AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbCrLf, "") AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbCr, "") AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbLf, "") AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), "$$", "$ $") MsgBox AR(ListBox1.ListIndex)

    End Sub


    Monday, March 31, 2014 2:53 PM

All replies

  • Hi,

    I assume the AR() is an array.

    I try to reproduce your issue in a user form. I find the first message box will pop up the different results as below. But the last message box shows the same result “aaaaa$bbb”. So I think the code works fine at last.

     

    In fact, using Enter button manually and word wrap are different though they look like same. When we click Enter button to create a new line, it will insert a vbCr after the previous line. We could test this from the picture below.

     

    If you want to show the same result in the first message box, I suggest you trying to edit the code as followed.

    Private Sub CommandButton1_Click()
    Dim AR(10) As Variant
    TextBox1.SetFocus
        LineCount = 0
        For Z = 0 To Len(TextBox1.Text) - 1
            TextBox1.SelStart = Z
            If TextBox1.CurLine > LineCount Then 'check to see if line has changed
                AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbCr, "")
                AR(ListBox1.ListIndex) = AR(ListBox1.ListIndex) & vbCr & "$"  '& Mid(TextBox1.Text, Z + 1, 1)
                MsgBox AR(ListBox1.ListIndex)
                LineCount = LineCount + 1
                Z = Z - 1
            Else
                AR(ListBox1.ListIndex) = AR(ListBox1.ListIndex) & Mid(TextBox1.Text, Z + 1, 1)
           End If
        Next
        
        'Cut manual new lines
        AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbNewLine, "")
        AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbCrLf, "")
        AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbCr, "")
        AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbLf, "")
        AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), "$$", "$ $")
        MsgBox AR(ListBox1.ListIndex)
    
    End Sub

    By the way, this forum is to discuss problems of Office development. But I think your issue is more related to VBA user form. For more questions about development of VBA, I suggest you post in VBA forum for more effective responses. 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 1, 2014 7:06 AM
    Moderator
  • Hi thanks for your reply.

    I have just had a look at this again and it is actually behaving correct with line wrap but not with enter press.
    Bellow are four pictures, the first is the string as it is loaded into the box, the second the string as it is saved with no editing, the third as the second string is loaded back in, and the fourth as it is saved with no editing once more. Notice the movement of the last $.

    1

    2

    (images 3+4 in next post)

    Here is the code for loading in the string to the textbox:

    Private Sub ListBox1_Change()
    ListBox1.Height = 108
    TextBox5.Text = ""
    If ListBox1.ListIndex > -1 Then
        Action = AR(ListBox1.ListIndex)
        For z = 1 To Len(Action)
            If Mid(Action, z, 1) = "$" Then
                trunc = Left(Action, z - 1)
                Action = Right(Action, Len(Action) - z)
                TextBox5.Text = TextBox5.Text & trunc & vbCr
                z = 1
            End If
        Next z
        TextBox5.Text = TextBox5.Text & Action
    End If
    End Sub

    And for saving back to the array AR()

    Private Sub CommandButton9_Click()
    If ListBox1.ListIndex > -1 Then
        'Check risk is selected
        MsgBox AR(ListBox1.ListIndex)
        If ListBox1.ListIndex > -1 Then
            'Update AR()
            AR(ListBox1.ListIndex) = ""
            TextBox5.SetFocus
            linecount = 0
             For z = 0 To Len(TextBox5.Text) - 1
                TextBox5.SelStart = z
                If TextBox5.CurLine > linecount Then 'check to see if line has changed
                    AR(ListBox1.ListIndex) = AR(ListBox1.ListIndex) & "$" & Mid(TextBox5.Text, z + 1, 1)
                    linecount = linecount + 1
                Else
                    AR(ListBox1.ListIndex) = AR(ListBox1.ListIndex) & Mid(TextBox5.Text, z + 1, 1)
               End If
            Next
            
            'Cut manual new lines
            AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbNewLine, "")
            AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbCrLf, "")
            AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbCr, "")
            AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), vbLf, "")
            AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), "$ ", "$")
            AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), " $", "$")
            AR(ListBox1.ListIndex) = Replace(AR(ListBox1.ListIndex), "$$", "$ $")
        
            MsgBox AR(ListBox1.ListIndex)
            'Repopulate Listbox1 from (AR)
            ListBox1.Clear
            For i = 0 To UBound(AR)
                    ListBox1.AddItem Chr(149) & Replace(AR(i), "$", "")
            Next
        End If
    End If
    End Sub

    Tuesday, April 1, 2014 10:27 AM
  • 3

    4

    Please feel free to move this to the correct forum

    • Edited by edkitch Tuesday, April 1, 2014 11:11 AM
    Tuesday, April 1, 2014 10:27 AM
  • I have updated the code for inputting the strings from the array to simplify things.

    Private Sub ListBox1_Change()
    ListBox1.Height = 108
    TextBox5.Text = ""
    If ListBox1.ListIndex > -1 Then
       TextBox5.Text = Replace(AR(ListBox1.ListIndex), "$", vbCr)
    End If
    End Sub

    The line wrap detection works perfectly, but when i start introducing manual carriage returns it all falls to pieces.

    Wednesday, April 2, 2014 11:18 AM