SelText 屬性返回包含選定文本的字符串。如果未選定任何文本,SelText 屬性值為 Null。String 型,可讀寫。
expression.SelText
expression 必需。返回“應(yīng)用于”列表中的一個(gè)對(duì)象的表達(dá)式。
SelText 屬性值為字符串表達(dá)式,該表達(dá)式中包含了控件中選定的文本。如果在設(shè)置該屬性時(shí),控件中已含有選定文本,則新的 SelText 設(shè)置將取代這些文本。
若要設(shè)置或返回控件的這個(gè)屬性,控件必須獲得焦點(diǎn)。要將焦點(diǎn)移到控件上,可以使用 SetFocus 方法。
下面的示例使用兩個(gè)事件過程來搜索用戶指定的文本。要搜索的文本在窗體 Load 事件過程中進(jìn)行設(shè)置?!安檎摇卑粹o(用戶單擊后可進(jìn)行搜索)的 Click 事件過程將提示用戶輸入要搜索的文本;如果搜索成功,則在文本框中選取該文本。
Sub Form_Load()
Dim ctlTextToSearch As Control
Set ctlTextToSearch = Forms!Form1!TextBox1
ctlTextToSearch.SetFocus ' SetFocus to text box.
ctlTextToSearch.SelText = "This company places large orders " _
& "twice a year for garlic, oregano, chilies and cumin."
End Sub
Sub Find_Click()
Dim strSearch As String, 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