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

Recommended Answers

All 6 Replies

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.

Hi,

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

Quite different, rather weird though.

Ragoune

*BUMP*

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.