Need line of found text in RichTextBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ImanMan
    New Member
    • Aug 2007
    • 5

    Need line of found text in RichTextBox

    Hi,

    if anyone can help me . i am displaying a textfile in a richTextBox. then i am implementing the search functionality. i need a code that would return for me the number of the line the search was found / or the index of the line( where in my file what i want to search for wasfound )
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I think you'll get what you want by combining GetLineFromChar method with the SelStart property.

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      Try this following sample code. It directly reads from the file not through a rich textbox, you may modify as required.

      [CODE=vb]Private Sub Command1_Click( )
      Dim n As String
      Dim ctr As Integer
      Dim CTR1 As Integer
      Dim STR As String
      Dim i As Integer
      Dim LNCOUNT As Integer
      Dim LNWORDCOUNT As Integer
      List1.Clear
      Label1.Caption = ""
      On Error GoTo err1
      cd.DialogTitle = "Open File"
      cd.InitDir = "d:\"
      cd.CancelError = True
      cd.ShowOpen
      T2 = Trim(Text1.Text )
      Open cd.FileName For Input As #1
      Do Until EOF(1)
      Line Input #1, n
      LNWORDCOUNT = 0
      LNCOUNT = LNCOUNT + 1
      T1 = n
      CT = 1
      Do While CT <= Len(T1)
      ctr = InStr(CT, T1, T2)
      If ctr <> 0 Then
      CT = ctr + 1
      CTR1 = CTR1 + 1
      LNWORDCOUNT = LNWORDCOUNT + 1
      Else
      Exit Do
      End If
      Loop
      'Print LNCOUNT & " " & LNWORDCOUNT
      If LNWORDCOUNT <> 0 Then
      List1.AddItem "Line No :" & LNCOUNT & " " & "Occurrence :" & LNWORDCOUNT
      End If
      Loop
      Label1.Caption = "Total Occurrences In File : " & CTR1
      Close #1
      Exit Sub
      err1:
      MsgBox Err.Description
      End Sub[/CODE]

      Hope that solves your problem.

      Comment

      Working...