Color text not returning to normal!

I have my text turning a different color when I type a certain thing in, but the problem is that it wont change back to black after I type it. And, after I type it, any key I press will retype what I typed before.

Heres my code:

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

Dim Tag As String

Tag = "<html>"

If RichTextBox1.Text.Contains(Tag) Then

Me.RichTextBox1.SelectedText = Tag

With RichTextBox1

.SelectAll()

.SelectionColor = Color.Blue

.DeselectAll()

.SelectionStart = Len(RichTextBox1.Text)

End With

RichTextBox1.ForeColor = Color.Black

End If

End Sub



Answer this question

Color text not returning to normal!

  • Karthik Krishnaswami

    I've been doing some research on the matter and based on Visual Studio 2003 sample codes from other developers i've cooked up the following code:

    Private bMatch As Boolean

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

    'Find the Word Currently under the Text Caret

    Dim iStart As Long

    Dim iEnd As Long

    Dim sKeyWord As String

    Dim iPos As Long

    With RichTextBox1

    iPos = .SelectionStart

    If iPos Then

    If Mid(.Text, iPos, 1) = "<" Then bMatch = True

    If Mid(.Text, iPos, 1) = ">" Then bMatch = False

    If bMatch Then

    iStart = InStrRev(.Text, "<", .SelectionStart)

    iEnd = InStr(iStart, .Text, "<", CompareMethod.Text)

    If iEnd = 0 Then iEnd = Len(.Text)

    'Check for a Match

    sKeyWord = Trim(Mid$(.Text, iStart, iEnd - iStart + 1))

    Me.Text = sKeyWord & " : " & Len(sKeyWord)

    .SelectionStart = iStart - 1

    .Select(.SelectionStart, Len(sKeyWord))

    .SelectionColor = Color.Blue

    .SelectionStart = iPos

    Else

    .SelectionColor = Color.Black

    End If

    End If

    End With

    End Sub

    It's still not perfect, since the code only works if you type in the tag at the end of the written text. If you were to start typing a tag in the middle of the sentence, the next character(s) will be overwritten and the color set back to black. I'm still working on that. In the meantime, maybe this bit of code will help with the selection issue. (^_^ )


  • OdieDenCO

    Shlizar Axis wrote:

    1. sKeyWord = Trim(Mid$(.Text, iStart, iEnd - iStart + 1))

    2. Me.Text = sKeyWord & " : " & Len(sKeyWord)

    .SelectionStart = iStart - 1

    It worked pretty good. but when ever im in the tag and delete a letter, if I type in it again the letters come out black.

    The 1st highlighted part is problematic. when Im debugging, an error comes up after a while and says that the Start cannot be equal to zero.

    and I dont believe I need the 2nd highlight because all it seems to do is change my Form title to what I type in the tag.

    1 othe thing, the check box only work to turn off the color but not put back on. Other then thoes 2 it works great.


  • Mandirigma1

    I've walked throught the code you mentioned within your previous post. It took me a while before i fully understood what the code did. I also noticed a few things which could be left out of the code or could be done with a different command. For instance, in his code he determined if the tag was upper cased or lower cased, like so:

    ' determine the tags case - upper or lower

    If Mid(sText, iStart + 1, 6) = "script" Then

    upperLower = "lower"

    isScript = True

    ElseIf Mid(sText, iStart + 1, 6) = "SCRIPT" Then

    upperLower = "upper"

    isScript = True

    ElseIf Mid(sText, iStart + 1, 4) = "html" Or Mid(sText, iStart + 1, 4) = "head" Or Mid(sText, iStart + 1, 4) = "body" Then

    upperLower = "lower"

    isHtml = True

    ElseIf Mid(sText, iStart + 1, 4) = "HTML" Or Mid(sText, iStart + 1, 4) = "HEAD" Or Mid(sText, iStart + 1, 4) = "BODY" Then

    upperLower = "upper"

    isHtml = True

    ElseIf Mid(sText, iStart + 1, 5) = "/html" Or Mid(sText, iStart + 1, 5) = "/head" Or Mid(sText, iStart + 1, 5) = "/body" Then

    upperLower = "lower"

    isHtml = True

    ElseIf Mid(sText, iStart + 1, 5) = "/HTML" Or Mid(sText, iStart + 1, 5) = "/HEAD" Or Mid(sText, iStart + 1, 5) = "/BODY" Then

    upperLower = "upper"

    isHtml = True

    ElseIf Mid(sText, iStart + 1, 5) = "style" Then

    upperLower = "lower"

    isStyle = True

    ElseIf Mid(sText, iStart + 1, 5) = "STYLE" Then

    upperLower = "upper"

    isStyle = True

    ElseIf Mid(sText, iStart + 1, 4) = "meta" Then

    upperLower = "lower"

    isMeta = True

    ElseIf Mid(sText, iStart + 1, 4) = "META" Then

    upperLower = "upper"

    isMeta = True

    ElseIf Mid(sText, iStart + 1, 6) = "applet" Then

    upperLower = "lower"

    isJava = True

    ElseIf Mid(sText, iStart + 1, 6) = "APPLET" Then

    upperLower = "upper"

    isJava = True

    ElseIf Mid(sText, iStart + 1, 3) = "img" Then

    upperLower = "lower"

    isImg = True

    ElseIf Mid(sText, iStart + 1, 3) = "IMG" Then

    upperLower = "upper"

    isImg = True

    End If

    In this particular sample he checked if the tag was Upper or Lower cased by checking it twice. This will generate more code then needed and you will need to do the same for every other tag you wish to verify (like the "<!--" tag). Also, if the user would type in "<Html>" the code would not be recognised. That's why i prefer to "convert" the text to 1 format and then check if the tag is being recognised. This is what i did:

    ' determine which tag has been found

    If LCase(InStr(1, sText, "script", CompareMethod.Text)) Then

    isScript = True

    ElseIf LCase(InStr(1, sText, "html", CompareMethod.Text)) Or LCase(InStr(1, sText, "head", CompareMethod.Text)) Or LCase(InStr(1, sText, "body", CompareMethod.Text)) Then

    isHtml = True

    ElseIf LCase(InStr(1, sText, "/html", CompareMethod.Text)) Or LCase(InStr(1, sText, "/head", CompareMethod.Text)) Or LCase(InStr(1, sText, "/body", CompareMethod.Text)) Then

    isHtml = True

    ElseIf LCase(InStr(1, sText, "style", CompareMethod.Text)) Then

    isStyle = True

    ElseIf LCase(InStr(1, sText, "meta", CompareMethod.Text)) Then

    isMeta = True

    ElseIf LCase(InStr(1, sText, "applet", CompareMethod.Text)) Then

    isJava = True

    ElseIf LCase(InStr(1, sText, "img", CompareMethod.Text)) Then

    isImg = True

    End If

    That way, if you want to add a new tag, you can insert the tag just above the "End If" statement. For instance:

    ...

    ElseIf LCase(InStr(1, sText, "applet", CompareMethod.Text)) Then

    isJava = True

    ElseIf LCase(InStr(1, sText, "img", CompareMethod.Text)) Then

    isImg = True

    ElseIf LCase(InStr(1, sText, "!--", CompareMethod.Text)) Then

    isComment = True

    End If

    I also removed the "Loop" statement since in the sample i've posted earlier i'm creating a selection via the "_TextChanged" procedure and providing the ColorHTML procedure with that particular selection. So, after implementing the code you posted in the previous message, my sample would look like this:

    Private bMatch As Boolean

    Public Function ColorHTML(ByVal sText As String)

    On Error GoTo ColorErr

    Dim iEnd As Long

    Dim isScript As Boolean

    Dim isStyle As Boolean

    Dim isLink As Boolean

    Dim isMeta As Boolean

    Dim isJava As Boolean

    Dim isImg As Boolean

    Dim isHtml As Boolean

    Dim isComment As Boolean

    ' determine which tag has been found

    If LCase(InStr(1, sText, "script", CompareMethod.Text)) Then

    isScript = True

    ElseIf LCase(InStr(1, sText, "html", CompareMethod.Text)) Or LCase(InStr(1, sText, "head", CompareMethod.Text)) Or LCase(InStr(1, sText, "body", CompareMethod.Text)) Then

    isHtml = True

    ElseIf LCase(InStr(1, sText, "/html", CompareMethod.Text)) Or LCase(InStr(1, sText, "/head", CompareMethod.Text)) Or LCase(InStr(1, sText, "/body", CompareMethod.Text)) Then

    isHtml = True

    ElseIf LCase(InStr(1, sText, "style", CompareMethod.Text)) Then

    isStyle = True

    ElseIf LCase(InStr(1, sText, "meta", CompareMethod.Text)) Then

    isMeta = True

    ElseIf LCase(InStr(1, sText, "applet", CompareMethod.Text)) Then

    isJava = True

    ElseIf LCase(InStr(1, sText, "img", CompareMethod.Text)) Then

    isImg = True

    ElseIf LCase(InStr(1, sText, "!--", CompareMethod.Text)) Then

    isComment = True

    End If

    ' color the tags

    If isScript = True Then

    Return Color.Red

    ' finished coloring script

    isScript = False

    ElseIf isMeta = True Then

    Return Color.Gray

    ' finished coloring meta

    isMeta = False

    ElseIf isStyle = True Then

    Return Color.Violet

    ' finished coloring style

    isStyle = False

    ElseIf isJava = True Then

    Return Color.SaddleBrown

    ' finished coloring java params

    isJava = False

    ElseIf isImg = True Then

    Return Color.Aqua

    ' finished coloring img

    isImg = False

    ElseIf isHtml = True Then

    Return Color.Blue

    ' finished coloring HTML

    isHtml = False

    ElseIf isComment = True Then

    Return Color.Green

    ' finished coloring HTML

    isComment = False

    Else

    ' its just plain old html

    Return Color.Blue

    End If

    Exit Function

    ColorErr:

    MsgBox("Error encountered at the last point of coloring" & vbCr & vbCr & "Error " & Err.Number & ": " & Err.Description, vbOKOnly + vbCritical, "Error " & Err.Number)

    Exit Function

    End Function

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    If CheckBox1.Checked = False Then

    RichTextBox1.SelectAll()

    RichTextBox1.SelectionColor = Color.Black

    RichTextBox1.SelectionStart = Len(RichTextBox1.Text)

    Else

    ' search for closing tags and color it

    End If

    RichTextBox1.Focus()

    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

    'Find the Word Currently under the Text Caret

    Dim iStart As Long

    Dim iEnd As Long

    Dim sKeyWord As String

    Dim iPos As Long

    Dim isColor As Color

    If CheckBox1.Checked = True Then

    With RichTextBox1

    iPos = .SelectionStart

    If iPos Then

    If Mid(.Text, iPos, 1) = "<" Then bMatch = True

    If bMatch Then

    iStart = InStrRev(.Text, "<", .SelectionStart)

    If iEnd = 0 Then iEnd = Len(.Text)

    'Check for a Match

    sKeyWord = Trim(Mid$(.Text, iStart, iEnd - iStart + 1))

    Me.Text = sKeyWord & " : " & Len(sKeyWord)

    .SelectionStart = iStart - 1

    .Select(.SelectionStart, Len(sKeyWord))

    '*** Color Check

    .SelectionColor = ColorHTML(.SelectedText)

    .SelectionStart = iPos

    Else

    .SelectionColor = Color.Black

    End If

    If Mid(.Text, iPos, 1) = ">" Then bMatch = False

    End If

    End With

    End If

    End Sub

    The only thing that's left is to initiate a loop (when marking the checkbox) which will check the entire text and provide the tags whith the appropriate color.


  • Andrea Williams

    Yea that code works great and I see what you mean by not perfected but its almost there. I have also set a check box to turn off and on the colored tags (I got it working but if you typed in colored tags before you checked it off, they are still colored while the new ones are black). So I am still working on that.

    Also, with this new code, is there anyway to implament other colors to other specific types of tags ie comment tags or java script tags ect.

    It will be interesting to see what is uncovered!


  • TrickyToo

    The problem is that if you use the "RichTextBox1.Text.Contains(Tag)" command, the application will always enter the IF statement. This is because once the "<HTML>" has been found, it will always be found, thus the text color will always be set to Blue.

    Try the following code:

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

    Dim Tag As String, CloseTag As String

    Tag = "<html>"

    If Len(RichTextBox1.Text) > Len(Tag) Then

    If Mid(RichTextBox1.Text, Len(RichTextBox1.Text) - Len(Tag), Len(Tag)) = Tag Then

    'Replaced Me.RichTextBox1.SelectedText = Tag with

    CloseTag = "</" & Mid(Tag, 2, Len(Tag) - 1)

    Me.RichTextBox1.Text = Me.RichTextBox1.Text & CloseTag

    Me.RichTextBox1.Select(RichTextBox1.TextLength - (Len(Tag) + Len(CloseTag)), (Len(Tag) + Len(CloseTag)))

    With RichTextBox1

    '.SelectAll() Removed

    .SelectionColor = Color.Blue

    .DeselectAll()

    .SelectionStart = Len(RichTextBox1.Text)

    End With

    RichTextBox1.SelectionColor = Color.Black

    End If

    End If

    End Sub

    I've changed the "RichTextBox1.Text.Contains(Tag)" command into a "Mid()" command which only verifies if the previous 6 characters resemble the Tag value.

    What i understood is that you would like to close the HTML tag once it's been found. However, while testing it, the application keeps on adding "<html><html>" after every character i type in once the Tag has been found. That's why i've assigend a "Close Tag" to a variable which will be populated with an "Close Tag" and added at the end of the RichTextBox so that the Tag will be closed after it's been found. After adding the "Close Tag", i would select both the Tag as well as the "Close Tag" and adjust the selectiontext to Blue. I've "removed" the SelectAll command, otherwise the entire text within the RichTextBox would probably be blue. (^_^ )


  • skycro

    If you would like to reset the textcolor to black, you could do the following:

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    If CheckBox1.Checked = False Then

    RichTextBox1.SelectAll()

    RichTextBox1.SelectionColor = Color.Black

    RichTextBox1.SelectionStart = Len(RichTextBox1.Text)

    End If

    End Sub

    Yes, it is possible to implement other colors as well. However, will they be mentioned between the same tags ("<" & ">") If you, you might need to filter the text and search for specific commands like JavaScript, comments, etc, etc. It all depends on what you want to do with it. (^_^ )


  • Ravenna Li

    Well, a guy over at xtremevbtalk.com suggested this...

    Public Sub ColorHTML(ByVal sText As String, ByVal fOutput As Form1)

    On Error GoTo ColorErr

    Dim iStart As Long

    Dim iEnd As Long

    Dim isScript As Boolean

    Dim isStyle As Boolean

    Dim isLink As Boolean

    Dim isMeta As Boolean

    Dim isJava As Boolean

    Dim isImg As Boolean

    Dim isHtml As Boolean

    Dim upperLower As String

    iStart = 1

    iEnd = 0

    fOutput.RichTextBox1.Enabled = False

    fOutput.RichTextBox1.Visible = False

    Screen.MousePointer = vbHourglass

    Do While InStr(iEnd + 1, sText, "&lt;")

    ' search for opening tag "&lt;"

    iStart = InStr(iEnd + 1, sText, "&lt;")

    ' update the progress

    Form1.prgBar.Value = (iStart / Len(sText)) * 100

    DoEvents()

    ' determine the tags case - upper or lower

    If Mid(sText, iStart + 1, 6) = "script" Then

    upperLower = "lower"

    isScript = True

    ElseIf Mid(sText, iStart + 1, 6) = "SCRIPT" Then

    upperLower = "upper"

    isScript = True

    ElseIf Mid(sText, iStart + 1, 4) = "html" Or Mid(sText, iStart + 1, 4) = "head" Or Mid(sText, iStart + 1, 4) = "body" Then

    upperLower = "lower"

    isHtml = True

    ElseIf Mid(sText, iStart + 1, 4) = "HTML" Or Mid(sText, iStart + 1, 4) = "HEAD" Or Mid(sText, iStart + 1, 4) = "BODY" Then

    upperLower = "upper"

    isHtml = True

    ElseIf Mid(sText, iStart + 1, 5) = "/html" Or Mid(sText, iStart + 1, 5) = "/head" Or Mid(sText, iStart + 1, 5) = "/body" Then

    upperLower = "lower"

    isHtml = True

    ElseIf Mid(sText, iStart + 1, 5) = "/HTML" Or Mid(sText, iStart + 1, 5) = "/HEAD" Or Mid(sText, iStart + 1, 5) = "/BODY" Then

    upperLower = "upper"

    isHtml = True

    ElseIf Mid(sText, iStart + 1, 5) = "style" Then

    upperLower = "lower"

    isStyle = True

    ElseIf Mid(sText, iStart + 1, 5) = "STYLE" Then

    upperLower = "upper"

    isStyle = True

    ElseIf Mid(sText, iStart + 1, 4) = "meta" Then

    upperLower = "lower"

    isMeta = True

    ElseIf Mid(sText, iStart + 1, 4) = "META" Then

    upperLower = "upper"

    isMeta = True

    ElseIf Mid(sText, iStart + 1, 6) = "applet" Then

    upperLower = "lower"

    isJava = True

    ElseIf Mid(sText, iStart + 1, 6) = "APPLET" Then

    upperLower = "upper"

    isJava = True

    ElseIf Mid(sText, iStart + 1, 3) = "img" Then

    upperLower = "lower"

    isImg = True

    ElseIf Mid(sText, iStart + 1, 3) = "IMG" Then

    upperLower = "upper"

    isImg = True

    End If

    ' search for closing tag "&gt;"

    ' if theres a closing tag

    If InStr(iStart + 1, sText, "&gt;") Then

    If isScript = True Then

    If upperLower = "upper" Then

    iEnd = InStr(iStart + 1, sText, UCase("&lt;/")) + 8

    Else

    iEnd = InStr(iStart + 1, sText, "&lt;/") + 8

    End If

    ElseIf isStyle = True Then

    If upperLower = "upper" Then

    iEnd = InStr(iStart + 1, sText, UCase("&lt;/")) + 7

    Else

    iEnd = InStr(iStart + 1, sText, "&lt;/") + 7

    End If

    ElseIf isJava = True Then

    If upperLower = "upper" Then

    iEnd = InStr(iStart + 1, sText, UCase("&lt;/")) + 8

    Else

    iEnd = InStr(iStart + 1, sText, "&lt;/") + 8

    End If

    ElseIf isMeta = True Or isImg = True Or isHtml = True Then

    iEnd = InStr(iStart + 1, sText, "&gt;")

    Else

    iEnd = InStr(iStart + 1, sText, "&gt;")

    End If

    Else ' didn't find a closing tag

    If Not InStr(iStart + 1, sText, "&lt;") Then ' if theres no opening tag

    Screen.MousePointer = vbDefault ' exit coloring

    Exit Sub

    End If

    End If

    ' color the tags

    fOutput.RichTextBox1.SelectionStart = iStart - 1

    fOutput.RichTextBox1.SelectionLength = (iEnd - iStart) + 1

    If isScript = True Then

    fOutput.RichTextBox1.SelectionColor = cScript

    ' finished coloring script

    isScript = False

    ElseIf isMeta = True Then

    fOutput.RichTextBox1.SelectionColor = cMeta

    ' finished coloring meta

    isMeta = False

    ElseIf isStyle = True Then

    fOutput.RichTextBox1.SelectionColor = cStyle

    ' finished coloring style

    isStyle = False

    ElseIf isJava = True Then

    fOutput.RichTextBox1.SelectionColor = cJava

    ' finished coloring java params

    isJava = False

    ElseIf isImg = True Then

    fOutput.RichTextBox1.SelectionColor = cImg

    ' finished coloring img

    isImg = False

    ElseIf isHtml = True Then

    fOutput.RichTextBox1.SelectionColor = cSpecial

    ' finished coloring HTML

    isHtml = False

    Else

    ' its just plain old html

    fOutput.RichTextBox1.SelectionColor = cHtml

    End If

    Loop

    isColored = True

    Screen.MousePointer = vbDefault

    fOutput.RichTextBox1.Enabled = True

    fOutput.RichTextBox1.Visible = True

    fOutput.RichTextBox1.SelectionStart = 0

    fOutput.prgBar.Value = 0

    Exit Sub

    ColorErr:

    MsgBox("Error encountered at the last point of coloring", vbOKOnly + vbCritical, "CodeCrawler")

    Screen.MousePointer = vbDefault

    fOutput.RichTextBox1.SelStart = 0

    fOutput.RichTextBox1.Enabled = True

    fOutput.RichTextBox1.Visible = True

    fOutput.prgBar.Value = 0

    Exit Sub

    End Sub

    (got that from here: http://www.xtremevbtalk.com/showthread.php t=12896)

    Maybe that will influence the code


  • Muruli

    I've been thinking about ways to define which text need to get which color. I've created a Function which will check the .SelectedText and verifies which color it should get.

    How about this:

    Private bMatch As Boolean

    Function ColorCheck(ByVal myText As String)

    If Len(myText) > 3 Then

    If Mid(myText, 1, 4) = "<!--" Then

    Return Color.Green

    Else

    Return Color.Blue

    End If

    Else

    Return Color.Blue

    End If

    End Function

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

    'Find the Word Currently under the Text Caret

    Dim iStart As Long

    Dim iEnd As Long

    Dim sKeyWord As String

    Dim iPos As Long

    Dim isColor As Color

    If CheckBox1.Checked = True Then

    With RichTextBox1

    iPos = .SelectionStart

    If iPos Then

    If Mid(.Text, iPos, 1) = "<" Then bMatch = True

    If Mid(.Text, iPos, 1) = ">" Then bMatch = False

    If bMatch Then

    iStart = InStrRev(.Text, "<", .SelectionStart)

    'iEnd = InStr(iStart, .Text, "<", CompareMethod.Text)

    If iEnd = 0 Then iEnd = Len(.Text)

    'Check for a Match

    sKeyWord = Trim(Mid$(.Text, iStart, iEnd - iStart + 1))

    Me.Text = sKeyWord & " : " & Len(sKeyWord)

    .SelectionStart = iStart - 1

    .Select(.SelectionStart, Len(sKeyWord))

    '*** Color Check

    .SelectionColor = ColorCheck(.SelectedText)

    .SelectionStart = iPos

    Else

    .SelectionColor = Color.Black

    End If

    End If

    End With

    End If

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    If CheckBox1.Checked = False Then

    RichTextBox1.SelectAll()

    RichTextBox1.SelectionColor = Color.Black

    RichTextBox1.SelectionStart = Len(RichTextBox1.Text)

    Else

    End If

    RichTextBox1.Focus()

    End Sub


  • sacredspirita

    All I need is the color blue between from "<" , to ">". "<!--" and "--//>" along with everything in between needs to be green. And about 5 more other words.

    Yea I might need to filter...


  • PavaniPolu

    A lot still needs to be done when it comes to this particular application. (^_^ ) It's true that when you go into a tag and type in a character, the text will be black. The reason behind it is that at that time the bMatch value is False, thus it will not check the tag. I haven't been looking into that yet.

    The top priority was to get a grasp of the basic functionality of the application, which has been roughly build with my previous sample. Now what has to be done is to extend the code to incorperate a "Search" throughout the entire test to see if a tag has been found and what color needs to be assigned to. Phase 2 of the application. (^_^ ) Once a procedure has been build which "searches" through the entire text and colored the selections accordingly, you just have to perform a procedure Call once the ChecBox has been checked.

    It's true that you don't need the 2nd Highlighted text. It's merely a command used for debugging. That way i can verify what text has been selected.

    I must say that i haven't had an error with the first highlighted text, but i can imagine that it would pop up when you would select a tag and type into it, since i haven't checked it yet. (^_^ )

    In other words... Let's get crackin'. Try to come up with a way to search through the text and find one tag.


  • Manic_Again

    That seemed to work with the exception of the very first part of the opening tag "<" is still black. And when I try to add another tag with the original, the original goes back to black while the new one gets a color change.

    I also added the comment tag in there and works but same concept applys.

    Dim html As String, Closehtml As String

    html = "<html>"

    If Len(RichTextBox1.Text) > Len(html) Then

    If Mid(RichTextBox1.Text, Len(RichTextBox1.Text) - Len(html), Len(html)) = html Then

    'Replaced Me.RichTextBox1.SelectedText = Tag with

    Closehtml = "</" & Mid(html, 2, Len(html) - 1)

    Me.RichTextBox1.Text = Me.RichTextBox1.Text & Closehtml

    Me.RichTextBox1.Select(RichTextBox1.TextLength - (Len(html) + Len(Closehtml)), (Len(html) + Len(Closehtml)))

    With RichTextBox1

    '.SelectAll() Removed

    .SelectionColor = Color.Blue

    .DeselectAll()

    .SelectionStart = Len(RichTextBox1.Text)

    End With

    RichTextBox1.SelectionColor = Color.Black

    End If

    End If

    Dim Comment As String, CloseComment As String

    Comment = "<!--"

    If Len(RichTextBox1.Text) > Len(Comment) Then

    If Mid(RichTextBox1.Text, Len(RichTextBox1.Text) - Len(Comment), Len(Comment)) = Comment Then

    'Replaced Me.RichTextBox1.SelectedText = Tag with

    CloseComment = "--//>" '& Mid(Comment, 2, Len(Comment) - 1)

    Me.RichTextBox1.Text = Me.RichTextBox1.Text & CloseComment

    Me.RichTextBox1.Select(RichTextBox1.TextLength - (Len(Comment) + Len(CloseComment)), (Len(Comment) + Len(CloseComment)))

    With RichTextBox1

    '.SelectAll() Removed

    .SelectionColor = Color.Green

    .DeselectAll()

    .SelectionStart = Len(RichTextBox1.Text)

    End With

    RichTextBox1.SelectionColor = Color.Black

    End If

    End If

    Is there a way to just tell it. when ever this key "<" is typed, then change color to blue along with everything else until this key ">" is typed


  • Color text not returning to normal!