Reputation: 32143
I've got to do some special things with a RichTextBox. I have to add syntax highlighting and I need to be able to find out what character was added/removed/inserted at what position every time a key is pressed. Is there some way to edit the existing, or is there a open source (.net compatible, preferably VB.net) available for download? I've tried making my own, the problem is, it has to have every function normally available and I don't have enough time to implement all of that.
Thanks!
Upvotes: 0
Views: 283
Reputation: 1846
There's no need to reinvent the wheel here. You have two options for doing this. The first is that you can hook into the events raised by your RichTextBox and do what you need there:
Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
'Add code to figure out what changed
'This will most likely involve an variable storing the original text and comparing it to what the
'RichTextBox now contains
End Sub
There are a few issues with this. If you have to use the functionality in a lot of forms, you start duplicating code everywhere. You'll also need some helper variables to track this data.
A better solution would be to create your own RichTextBox class. Obviously you don't want to have to start again from scratch, so you can instead inherit from the existing class and then extend it how you want.
Public Class MyRichTextBox
Inherits System.Windows.Forms.RichTextBox
Private oldText As String
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
MyBase.OnTextChanged(e)
If Me.Text <> oldText Then
'Figure out what changes were made
End If
oldText = Me.Text
End Sub
Public Sub SyntaxHighlighting()
'Add code here to highlight syntax within the textbox
End Sub
End Class
Once you've compiled MyRichTextBox, it should show up on the Toolbox tab and then you can drag & drop it onto your form.
Upvotes: 2