SelLength, SelStart, SelText Properties Example

This example enables the user to specify some text to search for and then searches for the text and selects it, if found. To try this example, paste the code into the Declarations section of a form that contains a wide TextBox control, and then press F5 and click the form.

Private Sub Form_Load ()
   Text1.Text = "Two of the peak human experiences"
   Text1.Text = Text1.Text & " are good food and classical music."
End Sub
Private Sub Form_Click ()
   Dim Search, Where   ' Declare variables.
   ' Get search string from user.
   Search = InputBox("Enter text to be found:")
   Where = InStr(Text1.Text, Search)   ' Find string in text.
   If Where Then   ' If found,
      Text1.SelStart = Where - 1   ' set selection start and
      Text1.SelLength = Len(Search)   ' set selection length.
   Else
      MsgBox "String not found."   ' Notify user.
   End If
End Sub

This example shows how the Clipboard object is used in cut, copy, paste, and delete operations. To try this example, create a form with a TextBox control and use the Menu Editor to create an Edit menu (for each of the commands, set the Caption property = Cut, Copy, Paste, and Delete, respectively; set the Name property = EditCut, EditCopy, EditPaste, and EditDelete, respectively).

Private Sub EditCut_Click ()
   ' Clear the contents of the Clipboard.
 Clipboard.Clear
   ' Copy selected text to Clipboard.
   ClipBoard.SetText Screen.ActiveControl.SelText
   ' Delete selected text.
   Screen.ActiveControl.SelText = ""
End Sub

Private Sub EditCopy_Click ()
   ' Clear the contents of the Clipboard.
 Clipboard.Clear
   ' Copy selected text to Clipboard.
   ClipBoard.SetText Screen.ActiveControl.SelText
End Sub

Private Sub EditPaste_Click ()
   ' Place text from Clipboard into active control.
   Screen.ActiveControl.SelText = ClipBoard.GetText ()
End Sub

Private Sub EditDelete_Click ()
' Delete selected text.
Screen.ActiveControl.SelText = ""
End Sub