Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Making an IDE type textbox

I want to make an IDE style textbox where the text is norm, except when a word I define appears, it changes that word only's font color eg black to blue. how could I do this?

Comments

  • JomacJomac Member Posts: 231
    : I want to make an IDE style textbox where the text is norm, except when a word I define appears, it changes that word only's font color eg black to blue. how could I do this?
    :

    You use the MS RichTextBox control, I gave it a test and it works well, code below:

    [code]
    Option Explicit

    Private Sub RichTextBox1_LostFocus()
    Call HighlightWord(RichTextBox1, "dog", vbRed)
    End Sub

    Private Sub HighlightWord(rtbTextBox As RichTextBox, ByVal sWord As String, Optional lColor As Long = vbBlue, Optional bFullWord As Boolean = False)
    Dim sText As String
    Dim lStart As Long
    Dim lLen As Long

    lLen = Len(sWord)

    If bFullWord Then sWord = " " & sWord & " "

    With rtbTextBox
    sText = .Text
    If bFullWord Then sText = " " & sText & " "

    ' reset colour
    .SelStart = 0
    .SelLength = Len(sText)
    .SelColor = vbWindowText

    ' check for and highlight all occurences of word
    lStart = InStr(1, sText, sWord, vbTextCompare) - 1
    Do While lStart > 0
    .SelStart = lStart
    .SelLength = lLen
    .SelColor = lColor

    lStart = InStr(lStart + lLen, sText, sWord, vbTextCompare) - 1
    Loop
    End With
    End Sub

    [/code]
    ______
    [size=2][b][blue]J[/blue]omac.[/b][/size]

Sign In or Register to comment.