Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How color mistake letters in the textbox? 3

Status
Not open for further replies.

FemPro

Programmer
Feb 15, 2008
28
GR
Hello, i am very new to vb 2005 and i need your help.
I have two textboxes on a form, the first named tb1 and the second tb2. The tb1 has a standart text at runtime and the users types in tb2 what they see in tb1.
I have a piece of code:

Private Sub Tb2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tb2.TextChanged
Dim i As Integer
Dim mistakes As Integer

mistakes=0

For i = 0 To Tb2.Text.Length - 1
If Tb2.Text.Length <> Tb1.Text.Length Then
If Tb2.Text(i) <> Tb1.Text(i) Then
mistakes = mistakes + 1
Else
Tb2.ForeColor = Color.Black
End If
End If
Next i
End Sub

This coce checks for mistakes in tb2 while the user types. Well, if the user types a mistake letter i want it to be red color but only the mistake letter not entire the text. So if for example the user does 3 mistakes i want only the three mistake letters to be red. Is it possible to do it? And how? I think i am missing something after the statement mistakes=mistakes+1 but what exactly? Any help will be much appreciated.

Thanks everyone
in advanced FemPro.
 
Thank you RiverGuy.
But how is it possible to do it if i use richtextbox?
 
Try this sample code.

Code:
        Dim rt As New RichTextBox
        rt.Text = "Hello World"
        rt.Select(5, 3)
        rt.SelectionColor = Color.Red
        Me.Controls.Add(rt)
 
Thank you much RiverGuy i want to ask you something? Does richtextbox provides autosize to fits its contents?
 
I'm not sure. I've never used a TextBox of any kind with Autosize. Labels yes, but TextBoxes, no. I prefer scrollbars with TextBoxes.
 
You could get the size of tb1 after the contents are loaded and make tb2 the same size on
 
Tperri you are great too, that works fine.

I have another question to do about it, i don't know if i must open a new topic. My question is how can i prevent user pressing backspace, delete, home, end, insert, pageup, pagedown and the arrow buttons in the richtextbox? Is it possible and how? Any help will be much appreciated.

Thanks again
everybody in advance.

 
keydown event do select case on e.keys.

-I hate Microsoft!
-Forever and always forward.
 
Thank you Sorwen, this doesn't help me as much as I want.
The what I want to do is not allow users press these keys so that he will be unable to delete whatever he writes in the richtextbox. I also what to prevent user paste in the richtextbox with ctrl+V. When the user presses something of these I want to happen nothing. I want the user unable to put the cursor where he wants in the text of richtextbox. For example, if the user presses backspace, i don't want to delete, if he presses home or pageup i dont want the cursor to be at the start, if he presses tab i don't want to write tab, if he press insert i dont want to be able to insert etc. I don't know how can i do it exactly.

Private Sub UsersRtb_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles UsersRtb.KeyDown
If e.KeyCode = Keys.Delete Then
'?????????
Else
‘?????????
End If
End Sub
 
Code:
Private Sub UsersRtb_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles UsersRtb.KeyDown
        If e.KeyCode = Keys.Delete Then
          [b]e.SuppressKeyPress = True[/b]
        Else
    ‘?????????        
        End If
End Sub


-I hate Microsoft!
-Forever and always forward.
 
Thank you much Sorwen, that helped me to handle the keys pressed by keyboard but what about when the user clicks or doubleclicks on the richtextbox? He will be able to set the cursor everywhere he wants inside the text and i don't want this. I want to be unable to click or doubleclick in richtextbox. In the mouseclick and mousedoubleclick events the suppresskeypress property doesn't provided as the handled too. So what can i do about this?
 
Not really sure on that as I've never tried. There is likely away to set the cursor position, but I'm not sure. This may work, but likely isn't the best way to do it. Put the following code in the MouseDown Event.

Code:
RichTextBox1.SelectionStart = Len(RichTextBox1.Text)
RichTextBox1.SelectionLength = 0

-I hate Microsoft!
-Forever and always forward.
 
Thank you again Sorwen, it works fine it prevents click and double click as i want. Why do you think that maybe this is not the best way to do it?
 
Because the selection start and length are for use for example if you were going to highlight a portion of text as if you would to copy it, you wanted to get a subset of text for special formatting, or etc. There isn't exactly anything wrong with it, but that isn't the use it was intended for. There could be some side effect I'm not aware of. There shouldn't be, but that is why I just warn it may not be the best way. Maybe I should say instead the proper way.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top