Modifying the .net RichTextBox



我需要对RichTextBox做一些特殊的事情。我必须添加语法突出显示,我需要能够找出什么字符被添加/删除/插入在什么位置每次按下一个键。是否有一些方法来编辑现有的,或者有一个开源(.net兼容,最好是VB.net)可供下载?我试过自己做,问题是,它必须有每个功能正常可用,我没有足够的时间来实现所有这些。

谢谢!

这里没有必要重新发明轮子。您有两种选择。首先,你可以钩到你的RichTextBox引发的事件,并做你需要的:

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

这里有一些问题。如果你必须在很多形式中使用这个功能,你就会开始到处复制代码。您还需要一些辅助变量来跟踪这些数据。

一个更好的解决方案是创建你自己的RichTextBox类。显然,您不希望从头开始,所以您可以从现有类继承,然后按照您的需要扩展它。
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

一旦你编译了MyRichTextBox,它应该显示在工具箱选项卡上,然后你可以拖动&把它放到表单上

最新更新