老师好!
在richtextbox中查找关键字,找到后高亮显示,且焦点转到被找到的内容。
在网上找到一段c#的代码,也转成.net的代码了,但是不能使用。麻烦老师看看代码。
谢谢!
c#代码:
Font f=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
string str="";
if (richTextBox1.Find(str)>0)
{
int pos=richTextBox1.Find(str);
richTextBox1.SelectionStart=pos;
richTextBox1.SelectionLength=str.Length;
richTextBox1.SelectionFont=f;
richTextBox1.SelectionColor=Color.Red;
}
.net代码:
Private Sub SurroundingSub()
Dim f As Font = New Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point)
Dim str As String = ""
If richTextBox1.Find(str) > 0 Then
Dim pos As Integer = richTextBox1.Find(str)
richTextBox1.SelectionStart = pos
richTextBox1.SelectionLength = str.Length
richTextBox1.SelectionFont = f
richTextBox1.SelectionColor = Color.Red
End If
End Sub
窗口创建RichTextBox控件:
Dim ct As New System.Windows.Forms.RichTextBox
ct.Dock = System.Windows.Forms.DockStyle.Fill
ct.Name ="RichTextBox1"
e.Form.Controls("Panel1").BaseControl.Controls.Add(ct)
ct.text = "ASDFGH"