Results 1 to 3 of 3

Thread: Change color of individual character in text box on error?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    Louisiana, USA
    Posts
    24

    Question 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

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    Louisiana, USA
    Posts
    24
    Thanks, Hack. Guess that explains why I have been unable to do it.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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:
    1. 'Posted by chrisjk
    2. 'http://forums.vb-world.net/showthread.php?s=&threadid=118386
    3.  
    4. Dim strWord As String
    5. Dim lPos As Long
    6.  
    7. strWord = "Hello"
    8.  
    9. lPos = InStr(1, RichTextBox1.Text, strWord, vbTextCompare)
    10.  
    11. If lPos > 0 Then
    12.     With RichTextBox1
    13.        .SelStart = lPos - 1
    14.        .SelLength = Len(strWord)
    15.        .SelColor = vbRed
    16.        .SelStart = Len(RichTextBox1.Text)
    17.     End With
    18. 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
  •  



Click Here to Expand Forum to Full Width