Visual Basic: RichTextBox Control

GetLineFromChar Method Example

This example finds a string in a RichTextBox control based on a word entered in a TextBox control. After it finds the specified string, it displays a message box that shows the number of the line containing the specified word. To try this example, put a RichTextBox control, a CommandButton control and a TextBox control on a form. Load a file into the RichTextBox, and paste this code into the General Declarations section of the form. Then run the example, enter a word in the TextBox, and click the CommandButton.

  Private Sub Command1_Click()
   Dim FoundPos As Integer
   Dim FoundLine As Integer
   ' Find the text specified in the TextBox control.
   FoundPos = RichTextBox1.Find(Text1.Text, , , rtfWholeWord)

   ' Show message based on whether the text was found or not.

   If FoundPos <> -1 Then
      ' Returns number of line containing found text.
      FoundLine = RichTextBox1.GetLineFromChar(FoundPos)
      MsgBox "Word found on line " & CStr(FoundLine)
   Else
      MsgBox "Word not found."
   End If
End Sub