SelLength 属性

expandtri全部显示

SelLength 属性指定或确定文本框或组合框的文本框部分的所选字符数。SelLength 属性使用从 0 到文本框或组合框的文本框部分中的总字符数之间的 Integer 型数值。

说明

可以通过或 Visual Basic 设置 SelLength 属性。

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

SelLength 属性设为一个小于 0 的数会造成运行时错误

示例

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