TextBox1.SelectionColor = Color.Red 提示 不是TextBox的成员怎么办
时间: 2024-02-07 11:02:28 浏览: 30
如果在 VB.NET 中使用 `TextBox` 控件时,没有 `SelectionColor` 属性,你可以通过使用 `RichTextBox` 控件来实现文本亮显的效果。下面是修改后的代码示例:
```vb
Imports System.Text.RegularExpressions
' 创建正则表达式模式
Dim pattern As String = "指定的字符或字符串"
' 创建一个 Regex 对象,用于匹配文本框中的内容
Dim regex As New Regex(pattern)
' 在 RichTextBox 中查找匹配项
Dim matches As MatchCollection = regex.Matches(RichTextBox1.Text)
' 循环遍历匹配项并进行亮显
For Each match As Match In matches
' 可以通过设置匹配项的字体颜色、背景色等方式进行亮显
RichTextBox1.Select(match.Index, match.Length)
RichTextBox1.SelectionColor = Color.Red
Next
```
在上述代码中,我们将 `TextBox1` 替换为 `RichTextBox1`,因为 `RichTextBox` 控件有一个名为 `SelectionColor` 的属性,可以用来设置选定文本的颜色。
请确保在使用 `RichTextBox` 控件之前,在你的窗体设计中将 `TextBox` 控件更改为 `RichTextBox` 控件,并将其命名为 `RichTextBox1`(或者你自己给它取的名字)。这样,你就可以使用 `SelectionColor` 属性来设置选定文本的颜色了。