请问下如何通过关键字查找并定位文本框的内容呢,而且还需要能够向上查找和向下查找
比如
Dim txt As WinForm.TextBox = e.Form.Controls("TextBox1")
Dim s As String = txt.text
Dim f As String = e.Form.Controls("TextBox2").text
Dim idx As Integer = vars("i")
If idx > 0
idx = s.IndexOf(f,idx)
End If
If idx > -1 Then
vars("i") = idx + f.Length
txt.SelectionStart = idx
txt.SelectionLength = f.Length
Dim d = txt.BaseControl
d.ScrollToCaret
Else
vars("i") = 0
End If
多谢了。还想问下,找到之后,可以让查找的字处于选择状态吗?或者高亮显示
测试过了。但是没有选择呀
这是滚动到那个位置了。
[此贴子已经被作者于2021/8/4 9:50:02编辑过]
下面代码就是选择的,不管文本框不处于输入焦点状态是看不到选择的
txt.SelectionStart = idx
txt.SelectionLength = f.Length
选择控件试试
If idx > -1 Then
vars("i") = idx + f.Length
txt.SelectionStart = idx
txt.SelectionLength = f.Length
Dim d = txt.BaseControl
d.ScrollToCaret
txt.select
Else
vars("i") = 0
End If
Dim txt As WinForm.TextBox = e.Form.Controls("TextBox1")
Dim s As String = txt.text
Dim f As String = e.Form.Controls("检索内容").text
Dim idx As Integer = vars("i")
If idx > 0 Then
idx = s.IndexOf(f, idx)
End If
If idx > -1 Then
vars("i") = idx + f.Length
txt.SelectionStart = idx
txt.SelectionLength = f.Length
Dim d = txt.BaseControl
d.ScrollToCaret
txt.select
Else
vars("i") = 0
End If
是全部高亮了 滚动到了对应位置 需要是关键词高亮 请教老师