代码高亮显示类开发vbnet语言

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

本文是我在学习过程中编写的,用到的技术简单,内容较粗糙。

所涉及到的源代码没有版权,可以任意的传播、复制及修改。

但请记得给我也发一份,大家可以相互学习。

我的邮箱
*****************
Highlighter类继承自System.Windows.Forms.RichTextBox,添加了几个属性和方法,如下
名称类别说明
New方法重写了RichTextBox的New,初始化了属性ScrollBars、WordWrap CodeColor属性设置代码的颜色
Regstr属性保留字、注释等的正则表达式
loadtextfile方法新增加载文本文件的方法
FormatString方法新增按正则表达式匹配设置代码颜色的方法
该类使用正则表达式来获得相应的字符串匹配项集合MatchCollection,再遍历该集合,从每个Match项来获得RichTextBox的Select方法的两个参数,选择起始位置和选择长度。

再用RichTextBox的SelectionColor来设置颜色(还可进行扩展,修改字体、字号等)。

源代码文件:
‘Class.vb
Imports System.Drawing
Imports Microsoft.VisualBasic.FileIO
Imports System.Text.RegularExpressions
Public Class highlighter
Inherits System.Windows.Forms.RichTextBox
'RichTextBox选择起点
Dim startpos As Integer
'RichTextBox选择长度
Dim l As Integer
'正则表达式匹配项
Dim regmatch As Match
'正则表达式匹配项集合
Dim regmatchcol As MatchCollection
'定义颜色
Dim c As Color = Color.Black
'保留字正则表达式
Dim rs As String = ""
Public Property CodeColor() As Color
Get
CodeColor = c
End Get
Set(ByVal value As Color)
c = value
End Set
End Property
Public Property Regstr() As String
Get
Regstr = rs
End Get
Set(ByVal value As String)
rs = value
End Set
End Property
Public Sub loadtextfile(ByVal filename As String)
Dim tmpstr As String = ""
If FileSystem.FileExists(filename) Then
FileOpen(1, filename, OpenMode.Input)
Do While Not EOF(1)
tmpstr = tmpstr & vbCrLf & LineInput(1)
Loop
MyBase.Text = tmpstr
End If
End Sub
'代码着色采用的方法是Regex与RichTextBox的Select、SelectionColor相配合。

'先由相应的正则表达式获得匹配项,再遍历所有匹配项,从各匹配项获得匹配字符的位置和长度。

'再用RichTextBox的Select选择当前匹配项,用SelectionColor修改字体及颜色。

'一般匹配的顺序是先保留字再注释
Public Sub FormatString()
regmatchcol = Regex.Matches(MyBase.Text, rs, RegexOptions.Multiline)
For Each regmatch In regmatchcol
startpos = regmatch.Index
l = regmatch.Length
MyBase.Select(startpos, l)
MyBase.SelectionColor = c
Next
End Sub
Public Sub New()
MyBase.New()
MyBase.ScrollBars = 19 'ForcedBoth
MyBase.WordWrap = False
End Sub
End Class
生成完成后就可以像使用RichTextBox一样,使用loadfile或loadtextfile载入代码文件,然后设置Highlighter类的regstr属性和codecolor属性,再调用formatstring方法对代码进行高亮显示。

代码高亮显示实例:
Script.txt文件内容:
Imports System
Imports System.Text
Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic.FileIO
Public Class Form1
'RichTextBox选择起点
Dim startpos As Integer
'RichTextBox选择长度
Dim l As Integer
'正则表达式匹配项
Dim regmatch As Match
'正则表达式匹配项集合
Dim regmatchcol As MatchCollection
'保留字正则表达式
Dim Parsestr As String = "\b(AddHandler|AddressOf|AndAlso|Alias" & _
"|And|Ansi|As|Assembly|Auto|Boolean|ByRef|Byte|ByVal|Call|Case" & _
"|Catch|CBool|CByte|CChar|CDate|CDec|CDbl|Char|CInt|Class|CLng|CObj" & _
"|Const|CShort|CSng|CStr|CType|Date|Decimal|Declare|Default|Delegate" & _
"|Dim|DirectCast|Do|Double|Each|Else|ElseIf|End|Enum|Erase|Error|Event" & _
"|Exit|False|Finally|For|Friend|Function|Get|GetType|GoTo|Handles|If" & _
"|Implements|Imports|In|Inherits|Integer|Interface|Is|Let|Lib|Like|Long" & _
"|Loop|Me|Mod|Module|MustInherit|MustOverride|MyBase|MyClass|Namespace" & _
"|New|Next|Not|Nothing|NotInheritable|NotOverridable|Object|On|Option" & _
"|Optional|Or|OrElse|Overloads|Overridable|Overrides|ParamArray|Preserve" & _
"|Private|Property|Protected|Public|RaiseEvent|ReadOnly|ReDim|RemoveHandler" & _
"|Resume|Return|Select|Set|Shadows|Shared|Short|Single|Static|Step|Stop" & _
"|String|Structure|Sub|SyncLock|Then|Throw|To|True|Try|TypeOf|Unicode" & _
"|Until|Variant|When|While|With|WithEvents|WriteOnly|Xor)\b"
'较常用的VB的注释内容正则表达式
Dim remstr As String = "'[\W\w]*?$\n"
'获取代码内字符串的正则表达式
Dim constr As String = """" & "[\w\W]*?" & """"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
formatstring()
End Sub
Public Sub formatstring()
'代码着色采用的方法是Regex与RichTextBox的Select、SelectionColor相配合。

'先由相应的正则表达式获得匹配项,再遍历所有匹配项,从各匹配项获得匹配字符的位置和长度。

'再用RichTextBox的Select选择当前匹配项,用SelectionColor修改字体及颜色。

'一般匹配的顺序是先保留字再注释最后字符串
'修改保留字
regmatchcol = Regex.Matches(RichTextBox1.Text, Parsestr)
For Each regmatch In regmatchcol
startpos = regmatch.Index
l = regmatch.Length
RichTextBox1.Select(startpos, l)
RichTextBox1.SelectionColor = Color.Blue
Next
'修改注释项,注意要设置为多行模式,不然无法成功
regmatchcol = Regex.Matches(RichTextBox1.Text, remstr, RegexOptions.Multiline)
For Each regmatch In regmatchcol
startpos = regmatch.Index
l = regmatch.Length
RichTextBox1.Select(startpos, l)
RichTextBox1.SelectionColor = Color.FromArgb(0, 0, 128, 0)
Next
'修改代码中的字符串
regmatchcol = Regex.Matches(RichTextBox1.Text, constr, RegexOptions.Multiline)
For Each regmatch In regmatchcol
startpos = regmatch.Index
l = regmatch.Length
RichTextBox1.Select(startpos, l)
RichTextBox1.SelectionColor = Color.FromArgb(0, 163, 21, 21)
Next
RichTextBox1.Select(0, 0)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tmpstr As String = ""
FileOpen(1, Application.StartupPath & "\script.txt", OpenMode.Input)
Do While Not EOF(1)
tmpstr = tmpstr & LineInput(1) & vbCrLf
Loop
FileClose(1)
RichTextBox1.Text = tmpstr
formatstring()
End Sub
End Class
打开VS,新建一个Windows应用程序,在工具箱点右键-选择项,在弹出的窗口点浏览,选择前面生成的文件,然后确定。

在窗体上添加一个按钮和一个Highlighter,在form1.vb中添加如下代码:
Imports System
Imports System.Text
Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic.FileIO
Public Class Form1
'保留字正则表达式
Dim Parsestr As String = "\b(AddHandler|AddressOf|AndAlso|Alias" & _
"|And|Ansi|As|Assembly|Auto|Boolean|ByRef|Byte|ByVal|Call|Case" & _
"|Catch|CBool|CByte|CChar|CDate|CDec|CDbl|Char|CInt|Class|CLng|CObj" & _
"|Const|CShort|CSng|CStr|CType|Date|Decimal|Declare|Default|Delegate" & _
"|Dim|DirectCast|Do|Double|Each|Else|ElseIf|End|Enum|Erase|Error|Event" & _
"|Exit|False|Finally|For|Friend|Function|Get|GetType|GoTo|Handles|If" & _
"|Implements|Imports|In|Inherits|Integer|Interface|Is|Let|Lib|Like|Long" & _
"|Loop|Me|Mod|Module|MustInherit|MustOverride|MyBase|MyClass|Namespace" & _
"|New|Next|Not|Nothing|NotInheritable|NotOverridable|Object|On|Option" & _
"|Optional|Or|OrElse|Overloads|Overridable|Overrides|ParamArray|Preserve" & _
"|Private|Property|Protected|Public|RaiseEvent|ReadOnly|ReDim|RemoveHandler" & _
"|Resume|Return|Select|Set|Shadows|Shared|Short|Single|Static|Step|Stop" & _
"|String|Structure|Sub|SyncLock|Then|Throw|To|True|Try|TypeOf|Unicode" & _
"|Until|Variant|When|While|With|WithEvents|WriteOnly|Xor)\b"
'较常用的VB的注释内容正则表达式
Dim remstr As String = "'[\W\w]*?$\n"
'获取代码内字符串的正则表达式
Dim constr As String = """" & "[\w\W]*?" & """"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Highlighter1.loadtextfile(Application.StartupPath & "\script.txt")
Highlighter1.CodeColor = Color.Blue
Highlighter1.Regstr = Parsestr
Highlighter1.FormatString()
Highlighter1.CodeColor = Color.FromArgb(0, 0, 128, 0)
Highlighter1.Regstr = remstr
Highlighter1.FormatString()
Highlighter1.CodeColor = Color.FromArgb(0, 163, 21, 21)
Highlighter1.Regstr = constr
Highlighter1.FormatString()
Highlighter1.Select(0, 0)
End Sub
End Class
运行结果:。

相关文档
最新文档