Hi,

You probably got annoyed when you read the title, it must have been asked a million times. But unfortunatly I cant get it working for myself. Ive been using this code to highlight the word "var":

Dim search As String = "var"
        Dim index As Integer = RichTextBox1.Text.IndexOf(search)

        If (index <> -1) Then
            RichTextBox1.Select(index, search.Length)
            RichTextBox1.SelectionFont = New Font(fontfamily, fontsize, FontStyle.Bold)
        End If

But it selects the word "var" and then afterwards, it makes the whole RichTextBox have this font.

Does someone know how to change this so that it only changes the font of the word "var"?

Ragoune

Hi,

I have checked the given code. It is working fine as you expected.

Here is the code;

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim search As String = "var"
        Dim index As Integer = RichTextBox1.Text.IndexOf(search)

        If (index <> -1) Then
            RichTextBox1.Select(index, search.Length)
            RichTextBox1.SelectionFont = New Font("Arial", 20, FontStyle.Bold)
        End If
    End Sub

I have attached the output screenshot. have a look.

Please let us know the result.

Attachments rtf.png 19.78 KB

Hi,

Thanks for your reply, Ive implemented the code and this is my result (see below).

Quite different, rather weird though.

Ragoune

Edited 6 Years Ago by Ragoune: n/a

Attachments error1.png 64.18 KB

First thing I thought of was that you may be overwritting the formatting with new text, or maybe placing the routine under the wrong control.

Hi,

Well, Ill just show you my complete code (it isnt much) because it looks like there must be the error:

Dim fontfamily As String = "Courier New"
    Dim fontsize As Integer = 10
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RichTextBox1.Text = "var surface_old, surface_new, width, height, fraction;" + Environment.NewLine
        RichTextBox1.Text += "surface_old = argument0;\n" + Environment.NewLine
        RichTextBox1.Text += "surface_new = argument1;" + Environment.NewLine
        RichTextBox1.Text += "width = argument2;" + Environment.NewLine
        RichTextBox1.Text += "height = argument3;" + Environment.NewLine
        RichTextBox1.Text += "fraction = argument4;" + Environment.NewLine
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim search As String = "var"
        Dim index As Integer = RichTextBox1.Text.IndexOf(search)

        If (index <> -1) Then
            RichTextBox1.Select(index, search.Length)
            RichTextBox1.SelectionFont = New Font("Arial", 20, FontStyle.Bold)
        End If
    End Sub

Ragoune

This works.

Dim fontfamily As String = "Courier New"
    Dim fontsize As Integer = 10
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RichTextBox1.Text = "var surface_old, surface_new, width, height, fraction;" + Environment.NewLine
        RichTextBox1.Text += "surface_old = argument0;\n" + Environment.NewLine
        RichTextBox1.Text += "surface_new = argument1;" + Environment.NewLine
        RichTextBox1.Text += "width = argument2;" + Environment.NewLine
        RichTextBox1.Text += "height = argument3;" + Environment.NewLine
        RichTextBox1.Text += "fraction = argument4;" + Environment.NewLine
        AddHandler RichTextBox1.TextChanged, AddressOf RichTextBox1_TextChanged
        RichTextBox1_TextChanged(Me, New System.EventArgs())
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles RichTextBox1.TextChanged
        Dim search As String = "var"
        Dim position As Integer = RichTextBox1.SelectionStart
        Dim index As Integer = RichTextBox1.Text.IndexOf(search)
        If (index <> -1) Then
            RichTextBox1.Select(index, search.Length)
            RichTextBox1.SelectionFont = New Font("Arial", 20, FontStyle.Bold)
            RichTextBox1.Font = New Font(fontfamily, fontsize, FontStyle.Regular)
            RichTextBox1.Select(position, 0)
        End If
    End Sub

Edited 6 Years Ago by thecoat: n/a

This article has been dead for over six months. Start a new discussion instead.