Click here to Skip to main content
15,040,743 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, im new at, what im trying to do is that textbox3 and texbox5 have a data, and i want to make a "compare" button, when i click it, it will collor they data diffrences with red, colored data will be shown in texbox3. the code from vb6(this codes works well btw):

Private Sub Command5_Click()
Dim a As Integer
Dim R1 As String
Dim r2 As String
Dim strimm As String
strimm = RichTextBox1.Text
 RichTextBox1 = Replace(strimm, vbCrLf, "")

strimm = RichTextBox2.Text
 RichTextBox2 = Replace(strimm, vbCrLf, "")
For a = 0 To Len(RichTextBox2)

RichTextBox1.SelStart = a
RichTextBox1.SelLength = 1
R1 = RichTextBox1.SelText
RichTextBox2.SelStart = a
RichTextBox2.SelLength = 1
r2 = RichTextBox2.SelText
If R1 <> r2 Then
RichTextBox1.SelColor = vbRed
End If
End Sub

What I have tried:

The code i tried in it colores whole data with red, if only 1 is not eqaul, thats an error, i need to only diffrence data colored):

Private Sub Command5_Click(sender As Object, e As EventArgs) Handles Command5.Click

        Dim a As Integer
        Dim R1 As String
        Dim r2 As String
        Dim strimm As String
        strimm = TextBox3.Text
        TextBox3.Text = Replace(strimm, vbCrLf, "")

        strimm = TextBox5.Text
        TextBox5.Text = Replace(strimm, vbCrLf, "")
        For a = 0 To TextBox5.TextLength

            TextBox3.SelectionStart = a
            TextBox3.SelectionLength = 1
            R1 = TextBox3.SelectedText
            TextBox5.SelectionStart = a
            TextBox5.SelectionLength = 1
            r2 = TextBox5.SelectedText
            If R1 <> r2 Then
                TextBox3.ForeColor = Color.Red
            End If
    End Sub
Updated 13-Mar-17 22:01pm

1 solution

Textboxes cannot be coloured except as a whole: they do not have any "individual word" formatting facilities.
To do that, you'd have to write your own TextBox control - or use the built in RichTextBox control which would work in much the same way as the one in your VB6 code...
JeezyWonder 14-Mar-17 20:37pm
I replaced textbox with richtextbox, result the same
OriginalGriff 15-Mar-17 6:00am
Well, unless you changed ForeColor to refer to the selected text like you did in the VB6 example, I'm not surprised...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900