Genau. Nur diese Funktion brauchen wir.
ich bin jetzt so weit, das ich ein ganz neuen Projekt angefangen um diese Funktion richtig verstehen. Und dann füge ich ein Benutzersteuerelement ein. ind einfüge diese Code ein:
Private hilightDictionary As New Hashtable() 'holds words & their color
Dim originalText As String 'Text right before change
Dim newText As String 'Text right after change
Private Sub checkDictionary()
If Me.RichTextBox1.Text.Length > 0 Then
Dim cPosition As Integer = getCursorPosition()
Me.RichTextBox1.Select(Me.RichTextBox1.Text.Length - 1, 1)
Me.RichTextBox1.Select(cPosition, 0)
Me.RichTextBox1.ScrollToCaret()
'Get the starting point of word in context
Dim i As Integer = cPosition
Dim kair As Char
Do Until i < 1 Or kair = " "
kair = Mid(Me.RichTextBox1.Text, i, 1)
i -= 1
If kair = " " Then 'And (cPosition - i) > 3
i += 2
End If
Loop
Dim startCheck As Integer = i
If startCheck <= 0 Then startCheck = 1
'Get the ending point of word in context
i = startCheck
kair = ""
Do Until i > Me.RichTextBox1.Text.Length Or kair = " "
kair = Mid(Me.RichTextBox1.Text, i, 1)
i += 1
If kair = " " Then
i -= 1
End If
Loop
Dim endCheck As Integer = i - startCheck
Dim wordString As String = removePunctuation(Mid( _
Me.RichTextBox1.Text, startCheck, endCheck))
Me.ParentForm.Text = wordString
End If
End Sub
Private Function getCursorPosition() As Integer
Dim i As Integer = 1
Dim location As Integer = 1
Do Until i = Me.RichTextBox1.Text.Length Or Me.RichTextBox1.Text.Length _
<= 0
If Mid(originalText, i, 1) = Mid(newText, i, 1) Then 'if char is =
' ignore it
location += 1
Else
If Len(newText) < Len(originalText) Then 'otherwise, backup and
' stop, cursor found.
location -= 1
End If
Exit Do
End If
i += 1
Loop
Return location
End Function
Private Function removePunctuation(ByVal someString As String) As String
Dim tempValue As String
tempValue = Replace(someString, ",", "")
tempValue = Replace(tempValue, "!", "")
tempValue = Replace(tempValue, "?", "")
tempValue = Replace(tempValue, ".", "")
tempValue = Replace(tempValue, ":", "")
Return tempValue
End Function
Private Sub RichTextBox1_KeyUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp
checkDictionary() ' scan at point of cursor for keywords
End Sub In Designer muss RichTextBox mit "RichTextBox1"- Name einfügen.
und dann Debugen und in Form1- einfügen. Und einschließend Starten. Wenn du Tippst- dann dieses Wort kommt auf dem FormText. |