msdn中关于SelLength、SelStart、SelText 属性示例中代码如下:Private Sub Form_Load ()
Text1.Text = "Two of the peak human experiences"
Text1.Text = Text1.Text & " are good food and classical music."
End Sub
Private Sub Form_Click ()
Dim Search, Where ' 声明变量。
' 获取需要查找的字符串。
Search = InputBox("Enter text to be found:")
Where = InStr(Text1.Text, Search) ' 在文本中查找字符串。
If Where Then ' 如果找到,
Text1.SelStart = Where - 1 ' 设置选定的起始位置并
Text1.SelLength = Len(Search) ' 设置选定的长度。
Else
MsgBox "String not found." ' 给出通知。
End If
End Sub 运行结果是能将找到的字符串处于被选中状态,就是蓝条显示 但是如果我将form_click事件改为command1_click(先添加一个按钮)事件后,就无法使之处于选中状态 为什么???
Text1.Text = "Two of the peak human experiences"
Text1.Text = Text1.Text & " are good food and classical music."
End Sub
Private Sub Form_Click ()
Dim Search, Where ' 声明变量。
' 获取需要查找的字符串。
Search = InputBox("Enter text to be found:")
Where = InStr(Text1.Text, Search) ' 在文本中查找字符串。
If Where Then ' 如果找到,
Text1.SelStart = Where - 1 ' 设置选定的起始位置并
Text1.SelLength = Len(Search) ' 设置选定的长度。
Else
MsgBox "String not found." ' 给出通知。
End If
End Sub 运行结果是能将找到的字符串处于被选中状态,就是蓝条显示 但是如果我将form_click事件改为command1_click(先添加一个按钮)事件后,就无法使之处于选中状态 为什么???
Text1.setfocus
Else这样就可以了,因为你的例子只有text1一个控件,inputbox后,焦点回到了text1上面,所以可以实现,你添加了command控件,焦点不会自动回去。