8 Replies - 7895 Views - Last Post: 20 January 2009 - 07:52 AM Rate Topic: -----

#1 mjsmikey   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-December 08

How to Use Get Character Line Postion

Posted 16 January 2009 - 12:06 PM

I need to know how to get a characters Line position in VB 2005

I search for string of text throught a RichTextBox and get outputs like 4086 and 16010 for the character postion. I need to know what lines of the document these fall on, so that I can properly copy those lines.

Could someone explain how to use this Class/Function.

Thanks,
MIKE
Is This A Good Question/Topic? 0
  • +

Replies To: How to Use Get Character Line Postion

#2 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How to Use Get Character Line Postion

Posted 16 January 2009 - 12:15 PM

Which class or function are you referring to?
Was This Post Helpful? 0
  • +
  • -

#3 mjsmikey   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-December 08

Re: How to Use Get Character Line Postion

Posted 16 January 2009 - 12:33 PM

Here is the problem... Its displaying the incorrect Line.
Text1 is M6. (I'm not sure if I use If Mid(...) correctly here. and I'm not sure If I use GetLineFromCharIndex correctly either.
For iPos = 1 To xy - 1
            If Mid(sString, iPos, Len(Text1)) = Text1 Then
                JJ = RichTextBox1.GetLineFromCharIndex(iPos) '- WHAT-? something here does not work correctly.
                ListBox1.Items.Add(JJ)
                Refresh()
            End If
        Next

'
Was This Post Helpful? 0
  • +
  • -

#4 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How to Use Get Character Line Postion

Posted 16 January 2009 - 02:13 PM

What is "Text1"?
Was This Post Helpful? 0
  • +
  • -

#5 mjsmikey   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-December 08

Re: How to Use Get Character Line Postion

Posted 16 January 2009 - 02:16 PM

Text1 is "M6"
...its text?

Is it possible that when I us RichTextBox1.Lines.Length I get a different number then when I use
RichTextBox1.GetLineFromCharIndex(Len(RichTextBox1.Text))
I get 18058 and 18451 as the Line Numbers. I cannot figure out why?

I seem to be getting Different Number? Does one of these Skip the BlanK lines? If so how do I fix it so they are both the same?
Was This Post Helpful? 0
  • +
  • -

#6 mjsmikey   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-December 08

Re: How to Use Get Character Line Postion

Posted 16 January 2009 - 02:26 PM

BTW Correct Number of Lines is 18058.... For Some Reason GetLineFrom... isn't working correctly... What do I use instead?
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to Use Get Character Line Postion

Posted 17 January 2009 - 01:09 AM

View Postmjsmikey, on 16 Jan, 2009 - 01:26 PM, said:

BTW Correct Number of Lines is 18058.... For Some Reason GetLineFrom... isn't working correctly... What do I use instead?

Read the RichTextBox documentation. Specifically RichTextBox.Find Method and
RichTextBox.GetLineFromCharIndex Method
The example code shows how to use them.
Was This Post Helpful? 0
  • +
  • -

#8 mjsmikey   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-December 08

Re: How to Use Get Character Line Postion

Posted 20 January 2009 - 05:14 AM

Well... Currently I already use the correct RichTextBox.GetLineFromCharIndex Method Correctly. As a matter of fact I got the code I use from this referenced site. It returns the same value 18451 for total lines when 18058 is the correct amount. I have also checked for word wrap and word wrap is set to false, so this shouldn't be causing the issue.

What else could cause incorrect Line readings?
Dim searchWord As String = "M6"
        Dim index As Integer = 0
        Dim iCount As Integer
        Dim Pos As Integer = 1
        Dim iPos
        Dim sString As String = RichTextBox1.Text ' String Given by User
        Dim Text1 As String = "M6"
        iCount = 0
        Dim xy As Integer = Len(sString)
        Dim JJ, y As Integer
        'Declare an ArrayList to store line numbers.
        Dim lineList As New System.Collections.ArrayList
        Do
            ' Find occurrences of the search word, incrementing  
            ' the start index. 
            index = RichTextBox1.Find(searchWord, index + 1, _
                RichTextBoxFinds.MatchCase)
            If (index <> -1) Then

                ' Find the word's line number and add the line 
                'number to the arrayList. 
                lineList.Add(RichTextBox1.GetLineFromCharIndex(index))
            End If
        Loop While (index <> -1)

        ' Iterate through the list and display the line numbers in TextBox1.
        Dim myEnumerator As System.Collections.IEnumerator = _
            lineList.GetEnumerator()
        If lineList.Count <= 0 Then
            RichTextBox2.Text &= searchWord & " was not found" & vbNewLine
        Else

            RichTextBox2.SelectedText &= "(THIS DOCUMENT HAS BEEN MODIFIED WITH STRIP-N-SAVE)" & vbNewLine & searchWord & " was found on line(s):" & vbNewLine
            While (myEnumerator.MoveNext)
                RichTextBox2.SelectedText = myEnumerator.Current & vbNewLine
            End While
        End If

Was This Post Helpful? 0
  • +
  • -

#9 mjsmikey   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-December 08

Re: How to Use Get Character Line Postion

Posted 20 January 2009 - 07:52 AM

I switched the Read Side to a Standard TextBox instead of RichTextBox and everything worked. But the Above code did not work with Textbox control only RichTextbox Control.

For Everyone Else... This Problem is Solve.
USE this code
            For iPos = 1 To xy
                If Mid(sString, iPos, Len(Text1)) = Text1 Or Mid(sString, iPos, Len(Text2)) = Text2 Then
                    JJ = TextBox1.GetLineFromCharIndex(iPos)
                    For k As Integer = JJ - LinesBefore To JJ + LinesAfter
                        If k >= TextBox1.Lines.Length Then
                            Exit For
                        End If
                        ListBox1.Items.Add(k)
                    Next
                    Refresh()
                End If
            Next

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1