Click to See Complete Forum and Search --> : Code Highlighter
WillemM
November 29th, 2002, 02:30 AM
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim pos1 As Integer
Dim pos2 As Integer
Dim ok As Boolean
ok = False
pos1 = RichTextBox1.SelectionStart
For pos2 = 0 To RichTextBox1.Text.Length - 1
RichTextBox1.Select(pos2, 1)
If RichTextBox1.SelectedText = "$" Then
RichTextBox1.SelectionColor = System.Drawing.Color.Red
End If
RichTextBox1.Select(pos2, 7)
If RichTextBox1.SelectedText = "_COOKIE" Then
RichTextBox1.SelectionColor = System.Drawing.Color.Blue
End If
Next
RichTextBox1.SelectionStart = pos1
RichTextBox1.SelectionLength = 0
RichTextBox1.SelectionColor = System.Drawing.Color.Black
End Sub
This function works very fine for me, but it starts flikkering when you type. Anyone who can tell me how to solve this ?
Athley
November 29th, 2002, 04:03 AM
Hehehe, this one is a bit DIRTY but it works, and it doesn't flicker anymore....
Dim counter As Integer = 0
Private Sub RichTextBox1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
Dim pos1, pos2 As Integer
Select Case e.KeyChar
Case "$"c
RichTextBox1.SelectionColor = Color.Red
Case "_"c
counter += 1
Case "C"c
If counter = 1 Then
counter += 1
End If
Case "O"c
If counter = 2 Or counter = 3 Then
counter += 1
End If
Case "K"c
If counter = 4 Then
counter += 1
End If
Case "I"c
If counter = 5 Then
counter += 1
End If
Case "E"c
If counter = 6 Then
pos1 = RichTextBox1.SelectionStart
pos2 = RichTextBox1.SelectionStart - counter
RichTextBox1.Select(pos2, counter)
RichTextBox1.SelectionColor = Color.Blue
RichTextBox1.SelectionStart = pos1
RichTextBox1.SelectionLength = 0
End If
Case Else
counter = 0
End Select
End Sub
Private Sub RichTextBox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp
RichTextBox1.SelectionColor = Color.Black
End Sub
If you consider this too dirty I think there should be an API to reduce flicker of this kind. Tell me and I'll try to find it.
/Leyan
WillemM
November 29th, 2002, 06:50 AM
There is one question: What if the user types instead of COOKIE COOL, it does make the letter COO blue.
I hope you can help me making some windows api code that reduses the flikker. This I think way to dirty. But it does reduce flikker a lot.
Athley
November 29th, 2002, 07:09 AM
It does? I only get the text blue if it is the exact text "_COOKIE", the counter should be keeping track of what you enter, and should be set to zero if you do not type in the correct order.
Well, as I admitted it sure was dirty....:)
There is a LockWindow API, but I haven't been able to get an example for you.... yet...... if I find anything I'll post it as soon as I find it.
/Leyan
Athley
November 29th, 2002, 07:19 AM
So, I found an example and implemented it with your code......
.... it doesn't flicker anymore..... the problem now is that if you start typing fast the box is not directly updated, so instead of flicker you get update problems if you type fast.
If you change the code just to do the check on Enter och Space it might run smoother.
Declare Function LockWindowUpdate Lib "user32" (ByVal hwnd As Long) As Long
Public Sub LockWindow(ByVal hwnd As Long)
LockWindowUpdate(hwnd)
End Sub
Public Sub UnlockWindow()
LockWindowUpdate(0)
End Sub
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim pos1 As Integer
Dim pos2 As Integer
Dim ok As Boolean
LockWindow(Me.Handle.ToInt32)
ok = False
pos1 = RichTextBox1.SelectionStart
For pos2 = 0 To RichTextBox1.Text.Length - 1
RichTextBox1.Select(pos2, 1)
If RichTextBox1.SelectedText = "$" Then
RichTextBox1.SelectionColor = System.Drawing.Color.Red
End If
RichTextBox1.Select(pos2, 7)
If RichTextBox1.SelectedText = "_COOKIE" Then
RichTextBox1.SelectionColor = System.Drawing.Color.Blue
End If
Next
RichTextBox1.SelectionStart = pos1
RichTextBox1.SelectionLength = 0
RichTextBox1.SelectionColor = System.Drawing.Color.Black
UnlockWindow()
End Sub
/Leyan
WillemM
November 29th, 2002, 09:39 AM
You got a fine solution there !
This works fine and even better when you only trigger the event on space, tab and enter keys.
Athley
November 29th, 2002, 10:12 AM
nice :)
/Leyan
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.