大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用一个全局变量c保存textbox1.SelectionStart,然后在textbox1.focus的代码后,将textbox1.SelectionStart设为c
创新互联建站是一家以网络技术公司,为中小企业提供网站维护、成都做网站、成都网站制作、成都外贸网站建设、网站备案、服务器租用、空间域名、软件开发、小程序开发等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站联系电话:028-86922220
TextChanged事件中捕获就可以。让后选择关键字文本定义颜色,如果关键词较多,并且有格式化文本要求,可以在程序外部定义Json或xml格式化定义文档,RichTextBox调用后根据定义格式化文本就好了。
SelectAll其实已经把所有文本选上了,只不过没有高亮而已,但是你可以自己模拟一个选择高亮:
r.SelectAll()
r.SelectionColor = System.Drawing.Color.White ' 文字色
r.SelectionBackColor = System.Drawing.Color.Navy ' 背景色
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim pos = RichTextBox1.SelectionStart
Dim i As String = Regex.Matches(RichTextBox1.Text, "\bDim\b").ToString
RichTextBox1.SelectAll
RichTextBox1.SelectionColor = Color.Black
If Regex.IsMatch(RichTextBox1.Text, "\bDim\b") = True Then
For Each mat As Match In Regex.Matches(RichTextBox1.Text, "\bDim\b")
RichTextBox1.SelectionStart = mat.Index
RichTextBox1.SelectionLength = mat.Length
RichTextBox1.SelectionColor = Color.YellowGreen
Next
End If
RichTextBox1.SelectionStart = pos
RichTextBox1.SelectionLength = 0
End Sub
我做了一个更详细的正则:
Friend Keys As String =
"\b(#Const|#If|Then|#Else|#Region|Delegate|Namespace|Class|End|Firend|Partial|Module|Interface|Enum|Shared|Overrides|Overloads|Structure|Let|Const|Dim|As|Private|Public|New|Static|Option|Private|Module|IsArray|IsDate|IsEmpty|IsError|IsMissing|IsNull|IsNumeric|IsObject|TypeName|VarType|Me|Option|Explicit|Mod|Like|Is|Not|And|Or|Xor|Eqv|Imp|Clear|Error|Raise|Error|Err|CVErr|On|Error|Resume|IsError|Collection|Add|Remove|Item|DDB|SLN|SYD|FV|Rate|IRR|MIRR|NPer|IPmt|Pmt|PPmt|NPV|PV|Do|Loop|For|Next|For|Each|Next|While|Wend|With|Choose|If|Then|Else|Select|Case|Switch|Call|Function|Property|Get|Property|Let|Property|Set|Sub|Date|Now|Time|DateAdd|DateDiff|DatePart|DateSerial|DateValue|TimeSerial|TimeValue|Date|Time|Timer|CBool|CByte|CCur|CDate|CDbl|CDec|CInt|CLng|CSng|CStr|CVar|CVErr|Fix|Int|Boolean|Byte|Currency|Date|Double|Integer|Long|Object|Single|String|Object|Atn|Cos|Sin|Tan|Exp|Log|Sqr|Randomize|Rnd|Abs|Sgn|Fix|Int|IsArray|Array|Option|Base|Dim|Private|Public|ReDim|Static|LBound|UBound|Erase|ReDim|DeleteSetting|GetSetting|GetAllSettings|SaveSetting|Chr|Format|LCase|UCase|DateSerial|DateValue|Hex|Oct|Format|Str|CBool|CByte|CCur|CDate|CDbl|CDec|CInt|CLng|CSng|CStr|CVar|CVErr|Fix|Int|Day|Month|Weekday|Year|Hour|Minute|Second|Asc|Val|TimeSerial|TimeValue|StrComp|StrConv|Format|LCase|UCase|Space|String|Len|Format|LSet|RSet|InStr|Left|LTrim|Mid|Right|RTrim|Trim|Option|Compare|Asc|Chr|AppActivate|Shell|SendKeys|Beep|Command)\b"
选中就是高亮,首先得到你找到的文本字符的起始位置和长度,然后
TextBox.Select(start,length)
或
TextBox.SelectionStart = start
TextBox.SelectionLength = length
'n为设置要选择的行,并高亮显示
ListView1.Items(n).Selected = True
ListView1.Focus()