SelText 属性

expandtri全部显示

SelText 属性返回包含选定文本的字符串。如果未选定任何文本,SelText 属性值为 NullString 型,可读写。

expression.SelText

expression     必需。返回“应用于”列表中的一个对象的表达式。

说明

SelText 属性值为字符串表达式,该表达式中包含了控件中选定的文本。如果在设置该属性时,控件中已含有选定文本,则新的 SelText 设置将取代这些文本。

若要设置或返回控件的这个属性,控件必须获得焦点。要将焦点移到控件上,可以使用 SetFocus 方法。

示例

下面的示例使用两个事件过程来搜索用户指定的文本。要搜索的文本在窗体 Load 事件过程中进行设置。“查找”按钮(用户单击后可进行搜索)的 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