SelLength 屬性指定或確定文本框或組合框的文本框部分的所選字符數(shù)。SelLength 屬性使用從 0 到文本框或組合框的文本框部分中的總字符數(shù)之間的 Integer 型數(shù)值。
可以通過宏或 Visual Basic 設(shè)置 SelLength 屬性。
若要設(shè)置或返回控件的這個(gè)屬性,控件必須獲得焦點(diǎn)。若要將焦點(diǎn)移到控件上,可以使用 SetFocus 方法。
將 SelLength 屬性設(shè)為一個(gè)小于 0 的數(shù)會造成運(yùn)行時(shí)錯(cuò)誤。
下面的示例使用兩個(gè)事件過程來搜索用戶指定的文本,要搜索的文本在窗體的 Load 事件過程中進(jìn)行設(shè)置?!安檎摇卑粹o(用戶單擊后可進(jìn)行搜索)的 Click 事件過程將提示用戶輸入要搜索的文本;如果搜索成功,則在文本框中選取該文本。
Private Sub Form_Load()
Dim ctlTextToSearch As Control
Set ctlTextToSearch = Forms!Form1!Textbox1
' SetFocus to text box.
ctlTextToSearch.SetFocus
ctlTextToSearch.Text = "This company places large orders twice " & _
"a year for garlic, oregano, chilies and cumin."
Set ctlTextToSearch = Nothing
End Sub
Public Sub Find_Click()
Dim strSearch As String
Dim intWhere As Integer
Dim ctlTextToSearch As Control
' Get search string from user.
With Me!Textbox1
strSearch = InputBox("Enter text to find:")
' Find string in text.
intWhere = InStr(.Value, strSearch)
If intWhere Then
' If found.
.SetFocus
.SelStart = intWhere - 1
.SelLength = Len(strSearch)
Else
' Notify user.
MsgBox "String not found."
End If
End With
End Sub