SelStart 屬性

此頁(yè)沒(méi)有內(nèi)容條目
內(nèi)容

expandtri全部顯示

SelStart 屬性指定或確定所選文本起始點(diǎn);或者在未選取任何文本時(shí)指定或確定插入點(diǎn)的位置。Integer 型,可讀寫(xiě)。

expression.SelStart

expression     必需。返回“應(yīng)用于”列表中的一個(gè)對(duì)象的表達(dá)式。

說(shuō)明

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

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

更改 SelStart 屬性會(huì)取消選定內(nèi)容,然后在文本中放置一個(gè)插入點(diǎn),并且將 SelLength 屬性設(shè)為 0。

示例

下面的示例使用兩個(gè)事件過(guò)程來(lái)搜索用戶指定的文本,要搜索的文本在窗體 Load 事件過(guò)程中進(jìn)行設(shè)置。“查找”按鈕(用戶單擊后可進(jìn)行搜索)的 Click 事件過(guò)程將提示用戶輸入要搜索的文本;如果搜索成功,則在文本框中選取該文本。

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