Visual Basic: RichTextBox Control

Span Method Example

This example defines a pair of keyboard shortcuts that selects text in a RichTextBox control to the end of a sentence (CTRL+S) or the end of a word (CTRL+W). To try this example, put a RichTextBox control on a form. Paste this code into the KeyUp event of the RichTextBox control. Then run the example.

  Private Sub RichTextBox1_KeyUp (KeyCode As Integer, Shift As Integer)
   If Shift = vbCtrlMask Then
      Select Case KeyCode
         ' If Ctrl+S:
         Case vbKeyS
            ' Select to the end of the sentence.
            RichTextBox1.Span ".?!:", True, True
            ' Extend selection to include punctuation.
            RichTextBox1.SelLength = RichTextBox1.SelLength + 1
         ' If Ctrl+W:
         Case vbKeyW
            ' Select to the end of the word.
            RichTextBox1.Span " ,;:.?!", True, True
      End Select
   End If
End Sub