- A+
想要自己开发一个编辑器软件,无论是代码还是其他用途,很多都用到关键字高亮。如RichTextBox1里面的关键字高亮,TextBox1关键字高亮
我在网上收集了三种代码可实现关键字高亮,中文注释,很好理解!
第一种!直接在RichTextBox1事件里面加入下面代码,在开头导入
Imports System.IO
Imports System.Text.RegularExpressions
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
Dim Keys As String = "\bGY |GX |NR1 |NR2 |NR3 |NR4 |SR1 |SR2 |SR3 |SR4 |B |N |L1 |L2 |L3 |L4 |D |DD |DDD \b" '关键字
Dim pos = RichTextBox1.SelectionStart
RichTextBox1.SelectAll()
RichTextBox1.SelectionColor = Color.Black
If Regex.IsMatch(RichTextBox1.Text, Keys) = True Then
For Each mat As Match In Regex.Matches(RichTextBox1.Text, Keys)
RichTextBox1.SelectionStart = mat.Index
RichTextBox1.SelectionLength = mat.Length
RichTextBox1.SelectionColor = Color.Blue
Next
End If
RichTextBox1.SelectionStart = pos
RichTextBox1.SelectionLength = 0
End Sub
复制
第二种!字定义函数,直接在读文件的时候调用,前提导入
Imports System.IO
Imports System.Text.RegularExpressions
Private Sub HighLightText()
Dim keywords() As String = {"B", "GY", "GX"} '关键字
Dim functions() As String = {"L1", "L2"} '关键字
Dim strings() As String = {"'((.|\n)*?)'"} '关键字
Dim whiteSpace() As String = {vbTab, vbLf, " "} '关键字
RichTextBox1.SelectAll()
RichTextBox1.SelectionColor = Color.Black
HighLightText(keywords, Color.Blue)
HighLightText(functions, Color.Magenta)
HighLightText(strings, Color.Red)
HighLightText(whiteSpace, Color.Black)
End Sub
Private Sub HighLightText(ByVal wordList() As String, ByVal color As Color)
For Each word As String In wordList
Dim r As New Regex(word, RegexOptions.IgnoreCase)
For Each m As Match In r.Matches(RichTextBox1.Text)
RichTextBox1.Select(m.Index, m.Length)
RichTextBox1.SelectionColor = color
Next m
Next word
End Sub
复制
调用,直接在按钮里面加入,如下
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sr1 As StreamReader = New StreamReader("C:\CAD\1.3B", System.Text.Encoding.Default)
Dim line1 As String = sr1.ReadToEnd()
RichTextBox1.Text = line1
sr1.Close()
HighLightText() '调用关键字高亮函数
End Sub
复制
第3种!
在开头加入
Imports System.IO
Imports System.Text.RegularExpressions
Imports HWND = System.IntPtr
在下加入代码Public Class Form1
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As IntPtr) As IntPtr
Private Const WM_SETREDRAW As Integer = &HB
Dim index As Integer = 0
Private Shared Function SendMessage(ByVal hwnd As HWND, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
End Function
Private Sub RichTextBox11_TextChanged(sender As System.Object, e As System.EventArgs) Handles RichTextBox1.TextChanged
SendMessage(RichTextBox1.Handle, WM_SETREDRAW, 0, IntPtr.Zero)
RichTextBox1.SelectAll()
RichTextBox1.SelectionColor = Color.Black
Dim keystr As String = "GY,GX,N,NR1,NR2,NR3,NR4,SR1,SR2,SR3,SR4,D,DD,L1,L2,L3,L4,B," '关键字
For i = 0 To UBound(keystr.Split(",")) - 1
Call GetKey(keystr.Split(",")(i), RichTextBox1.Text)
RichTextBox1.Select(Index, 0)
RichTextBox1.SelectionColor = Color.Black
Next
SendMessage(RichTextBox1.Handle, WM_SETREDRAW, 1, IntPtr.Zero)
'RichTextBox.Refresh()
End Sub
Public Function GetKey(ByVal P As String, ByVal S As String) As Integer
Dim CNT As Integer = 0
Dim M As Integer = P.Length
Dim N As Integer = S.Length
Dim SS As Char() = S.ToCharArray()
Dim PP As Char() = P.ToCharArray()
If M > N Then Return 0
For I = 0 To N - M + 1
Dim J As Integer = 0
For J = 0 To M
Try
If SS(I + J) <> PP(J) Then
Exit For
End If
Catch ex As Exception
End Try
If J = P.Length Then
RichTextBox1.Select(I, P.Length)
RichTextBox1.SelectionColor = Color.Blue
CNT = CNT + 1
End If
Next
Next
Return CNT
End Function
复制
感觉第3种反应速度有的慢。。。亲测都是可以实现关键字高亮,可以自定义关键字的