SelStart 屬性

此頁沒有內容條目
內容

expandtri全部顯示

SelStart 屬性指定或確定所選文本起始點;或者在未選取任何文本時指定或確定插入點的位置。Integer 型,可讀寫。

expression.SelStart

expression     必需。返回“應用于”列表中的一個對象的表達式。

說明

SelStart 屬性值為 Integer 型,其范圍從 0 到文本框或組合框的文本框部分中的總字符數(shù)。使用Visual Basic,可以設置 SelStart 屬性。

若要設置或返回控件的這個屬性,控件必須獲得焦點。要將焦點移到控件上,可以使用 SetFocus 方法。

更改 SelStart 屬性會取消選定內容,然后在文本中放置一個插入點,并且將 SelLength 屬性設為 0。

示例

下面的示例使用兩個事件過程來搜索用戶指定的文本,要搜索的文本在窗體 Load 事件過程中進行設置?!安檎摇卑粹o(用戶單擊后可進行搜索)的 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