|
-
Jan 2nd, 2002, 01:14 PM
#1
Thread Starter
Junior Member
Change color of individual character in text box on error?
I am using the following code to change the color of text in Text2.Text when the user makes an error entering text displayed in Text1.Text. I would like to change the color of the error character only and this code changes color of all characters in Text2.Text. Any suggestions? Thanks!
Private Sub Text2_KeyPress(KeyAscii As Integer)
Text2.SelStart = Len(Text2.Text)
Text2.ForeColor = vbBlack
'Next lines check for end of Text1 to avoid run time errors
If Len(Text1) > Len(Text2) Then
Text1.SelStart = Text2.SelStart
Text1.SelLength = 1
If Asc(Text1.SelText) <> KeyAscii Then
Text2.ForeColor = vbRed
End If
If Text2.ForeColor = vbRed Then
sndPlaySound "C:\Windows\MEDIA\DING.WAV", SND_ASYNC Or SND_NODEFAULT
End If
If KeyAscii = 8 Then 'Stops DING WAV on Backspace Key
sndPlaySound " ", SND_ASYNC Or SND_NODEFAULT
End If
'If Text1.SelStart = Len(Text2.Text) Then
If KeyAscii = 8 Then
Text2.ForeColor = vbBlack
End If
On Error Resume Next
End If
End Sub
-
Jan 2nd, 2002, 05:10 PM
#2
Thread Starter
Junior Member
Thanks, Hack. Guess that explains why I have been unable to do it.
-
Jan 3rd, 2002, 10:33 AM
#3
If decide to move to a richtextbox, here is some code that you might find useful in altering the color of individual words.
VB Code:
'Posted by chrisjk
'http://forums.vb-world.net/showthread.php?s=&threadid=118386
Dim strWord As String
Dim lPos As Long
strWord = "Hello"
lPos = InStr(1, RichTextBox1.Text, strWord, vbTextCompare)
If lPos > 0 Then
With RichTextBox1
.SelStart = lPos - 1
.SelLength = Len(strWord)
.SelColor = vbRed
.SelStart = Len(RichTextBox1.Text)
End With
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|