全部显示

SelStart 属性

       

SelStart 属性指定或确定所选文本起始点;或者在未选取任何文本时指定或确定插入点的位置。Integer 型,可读写。

expression.SelStart

expression   必需。返回“Applies To”列表中的一个对象的表达式。

说明

SelStart 属性值为 Integer 型,其范围从 0 到文本框或组合框的文本框部分中的总字符数。使用Visual Basic,可以设置 SelStart 属性。

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

更改 SelStart 属性会取消选定内容,然后在文本中放置一个插入点,并且将 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