Results 1 to 2 of 2

Thread: inserting a return in text

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    Post

    I've been fighting with this code for a few days now, so now I'll ask....
    The attached code checks each line of a rich text box for the word private (when I press return), if it finds it it changes its color and proceeds. Unfortunately, if you have typed out text and are editing that text I cannot insert a line in the middle of the document, instead it forces the cursor to the bottom of the document and inserts the return there.
    Anyone want to have a go at this?

    Private Sub HighlightText(sKeyword As String, iColour As Long)
    Dim nStart As Integer, sPrevChar As String, sNextChar As String

    nStart = InStr(1, LCase(TextBox1.Text), sKeyword)

    Do While nStart <> 0
    If nStart > 1 Then
    sPrevChar = Mid$(TextBox1.Text, nStart - 1, 1)
    Else
    sPrevChar = " "
    End If

    If Len(TextBox1.Text) >= nStart + Len(sKeyword) Then
    sNextChar = Mid$(TextBox1.Text, nStart + Len(sKeyword), 1)
    Else
    sNextChar = " "
    End If

    If (sPrevChar = Chr(32) Or sPrevChar = Chr(13) Or _
    sPrevChar = Chr(10) Or sPrevChar = Chr(9)) And _
    (sNextChar = Chr(32) Or sNextChar = Chr(13) Or _
    sNextChar = Chr(10) Or sNextChar = Chr(9)) Then
    'If word is from lowercase list then lowercase highlight word.
    If lowercase = True Then
    With TextBox1
    .SelStart = nStart - 1
    .SelLength = Len(sKeyword)
    .SelColor = iColour
    'Sets all text of selected word to lowercase.
    .SelText = StrConv(sKeyword, vbLowerCase)
    .SelStart = Len(TextBox1.Text)
    .SelColor = iColour
    End With
    'Else propercase the highlightword.
    Else:
    With TextBox1
    .SelStart = nStart - 1
    .SelLength = Len(sKeyword)
    .SelColor = iColour
    'Sets all text of selected word to lowercase.
    .SelText = StrConv(sKeyword, vbProperCase)
    .SelStart = Len(TextBox1.Text)
    .SelColor = iColour
    End With
    End If
    End If

    nStart = InStr(nStart + Len(sKeyword), LCase(TextBox1.Text), sKeyword)
    Loop

    End Sub
    Private Sub TextBox1_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
    With TextBox1
    .SelLength = Len(.Text)
    .SelColor = vbBlack
    If .SelStart = Len(.Text) Then
    'If at the end insert a return.
    .SelStart = Len(.Text)
    Else:
    ''''''''If not at the end insert return at present location.
    End If
    End With
    'Run highlight script.
    HighlightText "private sub", vbBlue
    'Reset text.
    TextBox1.SelColor = vbBlack
    End If
    End Sub

  2. #2
    Guest
    Are you not happy with your other thread? MartinLiss had a good way to do this.

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