Overwrite the contents in the rich text box while editing(in VB6.0)

I am using a rich text box.
Rich text box displays the contents from a text file.
I want user to write in the rich text box but no new character. Already displayed content should be over written and if he types correctly then it's color should be blue otherwise red.
For example
In text box following contentx are displayed

ad sdf dfg

user types

ad dfggdfg

now first two characters ad are correctly types so they should appear in blue.
At 4th position instead of 's' he types 'd' so 'd' should be displayed in red and it should over write the 's'.
Now if user presses back space then original contents should appear
How to perform this?
Give the logic
I want to do this in VB6.0
rajivAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
Wileecoy:

>> in this case I am
storing the .Text value in the .Tag property of the rtb in the form_load

I guess this is the bit you missed!
0
 
OtanaCommented:
Use a second, invisible Rich TextBox in which you keep the original content.

Use the keypress event, check the location of your cursor, compare to character in original content.
0
 
TimCotteeHead of Software ServicesCommented:
This should do it for you, the comparison method as otana suggest is the way to go, in this case I am storing the .Text value in the .Tag property of the rtb in the form_load, you could do this wherever appropriate.

Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case Asc("A") To Asc("Z"), Asc("a") To Asc("z"), Asc("0") To Asc("9"), 32
        With RichTextBox1
            CurrentChar = Chr(KeyCode)
            If Shift = 0 Then CurrentChar = LCase(CurrentChar)
            If CurrentChar = Mid(.Tag, .SelStart + 1, 1) Then
                .SelLength = 1
                .SelColor = vbBlue
                .SelText = IIf(.SelStart >= Len(.Tag), "", CurrentChar)
            Else
                .SelLength = 1
                .SelColor = vbRed
                .SelText = IIf(.SelStart >= Len(.Tag), "", CurrentChar)
            End If
        End With
        KeyCode = 0
    Case 8
        With RichTextBox1
            .SelStart = .SelStart - 1
            .SelLength = 1
            .SelColor = vbBlack
            .SelText = Mid(.Tag, .SelStart + 1, 1)
            .SelStart = .SelStart - 1
        End With
        KeyCode = 0
    Case 33 To 40
    Case Else
        KeyCode = 0
    End Select
End Sub
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
wileecoyCommented:
TimCottee,

I tried your code and was unable to get it to work.

I found that .Tag was always "" as a value was never set to it.

Is there additional code that you forgot to post?

Wileecoy.
0
 
wileecoyCommented:
oh - you mean all those words above the code?? lol.
0
 
wileecoyCommented:
Excellent - it works now! - mostly.

I am trying to code in "," and "-".

I'm not sure if rajiv will have a need for these - I have no need, but would make coding more complete.

Let us know rajiv.

This solution looks good.

Wileecoy.
0
 
DanRollinsCommented:
Hi rajiv@devx,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept TimCottee's comment(s) as an answer.

rajiv@devx, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
Computer101Commented:
Comment from expert accepted as answer

Computer101
E-E Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.