ComboBox.SelText 屬性 (Access)ComboBox.SelText property (Access)

SelText 屬性會傳回包含所選的文字的字串。The SelText property returns a string containing the selected text. 可讀寫 的字串Read/write String.

語法Syntax

運算式SelTextexpression.SelText

_運算式_代表**ComboBox** 物件的變數。expression A variable that represents a ComboBox object.

註解Remarks

如果未不選取任何文字, SelText 屬性會包含 Null 值。If no text is selected, the SelText property contains a Null value.

SelText 屬性會使用包含在控制項中選取之文字的字串運算式。The SelText property uses a string expression that contains the text selected in the control. 如果控制項包含選取的文字設定此屬性時,所選的文字會由新的 SelText 設定取代。If the control contains selected text when this property is set, the selected text is replaced by the new SelText setting.

設定或傳回控制項的這個屬性,控制項必須具有焦點。To set or return this property for a control, the control must have the focus. 若要將焦點移到控制項、 使用 SetFocus 方法。To move the focus to a control, use the SetFocus method.

範例Example

下列範例會使用兩個事件程序來搜尋的使用者所指定的文字。The following example uses two event procedures to search for text specified by a user. 要搜尋的文字是設定在表單的Load事件程序。The text to search is set in the form's Load event procedure. 找出] 按鈕 (使用者按一下 [開始搜尋)按一下 [ 事件程序會提示使用者輸入文字來搜尋,並在 [文字] 方塊中選取的文字,如果搜尋成功。The Click event procedure for the Find button (which the user clicks to start the search) prompts the user for the text to search for and selects the text in the text box if the search is successful.

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

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.