Page 1 of 1

RichTextbox colors

Posted: Mon Aug 06, 2012 6:22 pm
by AnoPem
I want to color every thing that is between "<" ">" those 2 symbols, like "<body>"
Does anyone know this can be achived with a richtextbox?

Re: RichTextbox colors

Posted: Mon Aug 06, 2012 6:24 pm
by codedrive
AnoPem wrote:
I want to color every thing that is between "<" ">" those 2 symbols, like "<body>"
Does anyone know this can be achived with a richtextbox?
Get a timer and make it enabled, with invertal of 100 then double click in in designer.vb and add the code,
Code: Select all
If Richtextbox1.text="TEXT" then
richtextbox1.text = color.blue
Something like that i think!

Re: RichTextbox colors

Posted: Mon Aug 06, 2012 6:30 pm
by AnoPem
codedrive wrote:
AnoPem wrote:
I want to color every thing that is between "<" ">" those 2 symbols, like "<body>"
Does anyone know this can be achived with a richtextbox?
Get a timer and make it enabled, with invertal of 100 then double click in in designer.vb and add the code,
Code: Select all
If Richtextbox1.text="TEXT" then
richtextbox1.text = color.blue
Something like that i think!
I want an easier way so it just pain everything between "<>"

Re: RichTextbox colors

Posted: Tue Aug 07, 2012 9:40 pm
by mandai
I don't think there is any need to use a timer.

You can use this to highlight the text between the tags:
Code: Select all
    Private Sub btnHighlight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHighlight.Click
        RichTextBox1.Select(0, RichTextBox1.Text.Length)
        RichTextBox1.SelectionColor = Color.Black

        Dim startFrom As Integer = 0
        Dim nameLength As Integer = 0

        For i As Integer = 0 To RichTextBox1.Text.Length - 1
            If RichTextBox1.Text(i) = "<"c Then
                startFrom = i
            ElseIf RichTextBox1.Text(i) = ">"c Then
                nameLength = i - startFrom - 1

                If nameLength > 0 Then
                    startFrom += 1
                    RichTextBox1.Select(startFrom, nameLength)
                    RichTextBox1.SelectionColor = Color.Blue
                End If

            End If

        Next
        RichTextBox1.SelectionStart = RichTextBox1.Text.Length
    End Sub

Re: RichTextbox colors

Posted: Tue Aug 07, 2012 9:50 pm
by muttley1968
I think you could do it a lot simpler with this
Code: Select all
if richtextbox1.text = "<"
then richtextbox1.forcolour = color.white 
elseif richtextbox1.text = ">" then 
richtextbox1.forcolour = color.blue 
PS I wrote this in browser and haven't tested it I not sure if it will or will not work might change the hole text color though but try what the worst that could happen

Re: RichTextbox colors

Posted: Tue Aug 07, 2012 10:04 pm
by mandai
@muttley1968 There seem to be some issues with that code i.e. missing End If etc.
If you are just looking to set the Forecolor then you could use simpler code, but it wouldn't be ideal for syntax highlighting.

Re: RichTextbox colors

Posted: Tue Aug 07, 2012 10:16 pm
by muttley1968
sorry I wrote off top of head in browser so my mad and I think forcolor was all he was talking bout