Results 1 to 11 of 11

Thread: Get last line in a TextBox

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Get last line in a TextBox

    I don't know why but I'm having an issue with getting the previous line with a return.

    What I mean is I can use CurrentLine() to get the current line and CurrentLine()-1 to get the previous line. What I want to do is to get the previous line that has a \n because with line wrap, CurrentLine()-1 gives you the previous line and not the beginning of the wrap.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get last line in a TextBox

    It's unclear to me what you want. Are you saying you want a line as indicated by line breaks or as indicated by word wrap? The wrapping has no bearing on the contents of the TextBox and is only relevant to the display, so it is not exposed through properties. You would use the Lines property to get a specific line based on line breaks.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Get last line in a TextBox

    Quote Originally Posted by jmcilhinney
    It's unclear to me what you want. Are you saying you want a line as indicated by line breaks or as indicated by word wrap? The wrapping has no bearing on the contents of the TextBox and is only relevant to the display, so it is not exposed through properties. You would use the Lines property to get a specific line based on line breaks.
    The Lines property only gets specific Lines according to how it's displayed. It doesn't matter if it has a '\n' or not.

    If you create a RichTextBox and then type a long paragraph with no line breaks (\n) with word wrap on. Then, when you press enter and use code like this:
    VB Code:
    1. MyTextBox.Lines[CurrentLine()-1];
    That will get the previous line (the last line in your paragraph) and not the entire paragraph as you'd expect if it was dividing the text with Line breaks (\n).

    Basically, what I want to do, is get the line before the CurrentLine() that has followed a line break (\n) because I need to count the white space at the beginning of the line (like how many tabs were used).
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get last line in a TextBox

    I'm not quite sure what's going on there but here's proof that the Lines property is based on hard line breaks. The screenshot below is from a C# 2005 Express project but I tried the same thing in VB 2005 Express and a VS.NET 2003 C# project and they all behaved the same way.
    Attached Images Attached Images  
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Get last line in a TextBox

    Then how can I get the correct Current line? CurrentLine() does not work by Line breaks but by visual lines. I'm more worried about getting the previous line though...

    I put a statusbar on a form with a richtestbox and if I keep typing with line wrap, CurrentLine() constantly increases but Lines.Length does not.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get last line in a TextBox

    This CurrentLine method you're referring to is not a member of the RichTextBox class. Are you referring to the method posted on Developer Fusion by James Crowley that uses the GetLineFromCharIndex method? I wasn't aware that GetLineFromCharIndex worked the way it does, but you can easily put that to use. If you want the text from the line above the current caret position you would do something like this:
    VB Code:
    1. Dim previousLineIndex As Integer = Me.RichTextBox1.GetLineFromCharIndex(Me.RichTextBox1.SelectionStart) - 1
    2.         Dim lastCharIndex As Integer = Me.RichTextBox1.SelectionStart - 1
    3.  
    4.         While Me.RichTextBox1.GetLineFromCharIndex(Me.RichTextBox1.SelectionStart) > previousLineIndex
    5.             lastCharIndex -= 1
    6.         End While
    7.  
    8.         Dim firstCharIndex As Integer = lastCharIndex
    9.  
    10.         While Me.RichTextBox1.GetLineFromCharIndex(firstCharIndex - 1) = previousLineIndex
    11.             firstCharIndex -= 1
    12.         End While
    13.  
    14.         Dim previousLine As String = Me.RichTextBox1.Text.Substring(firstCharIndex, lastCharIndex - firstCharIndex)
    I believe the .NET 2.0 RTB has some additional members, some of which may prove useful in this too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Get last line in a TextBox

    Your code doesn't work. First off, this is the C# forum (guess you forgot, lol). I translated it into C# but then I realized the first loop is infinate.

    After that, I fixed that and it worked exactly like my last method. It still didn't work correctly if there were wrapped lines. It couldn't get the beginning of the wrap as the last line.

    What I'm basically doing is making an automatic indent feature that indents as much on the next line as there was on the last line. To do this I was basically finding the previous line when the user hit enter and then counted the amount of white space (tabs and spaces) and then inserted it into the TextBox where the caret was.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get last line in a TextBox

    Quote Originally Posted by kasracer
    Your code doesn't work. First off, this is the C# forum (guess you forgot, lol). I translated it into C# but then I realized the first loop is infinate.

    After that, I fixed that and it worked exactly like my last method. It still didn't work correctly if there were wrapped lines. It couldn't get the beginning of the wrap as the last line.

    What I'm basically doing is making an automatic indent feature that indents as much on the next line as there was on the last line. To do this I was basically finding the previous line when the user hit enter and then counted the amount of white space (tabs and spaces) and then inserted it into the TextBox where the caret was.
    That's just rude. Just because it doesn't work is no reason to say it doesn't work. That first loop should have been using lastCharIndex rather than SelectionStart. Oops. Whenever I get a notification e-mail I always neglect to check whether it's from the C# forum, and given that it's about 15 VB.NET to 1 C# I always just assume. Oops again. I have to admit that I didn't test that code at all. It was just an approximation of something that I think should work. That's reason enough to believe that it will, isn't it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Get last line in a TextBox

    heh

    any ideas how to accomplish this? I tried going through and checking the previous lines for a '\n', however; it would appear that the RichTextBox does not contain any \n or Environment.NewLines so I'm not sure how to approach this
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  10. #10

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Get last line in a TextBox

    Anyone have any suggestions?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  11. #11
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: Get last line in a TextBox

    This will get the last line of a text box. lineCount - 1 controls what line of the textbox is returned.

    Code:
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage_Ex(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);
    
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
    
        private const int EM_GETLINECOUNT = 0x00BA;
    
        private const int EM_GETLINE = 0xc4;
    
        private void cmdTest_Click(object sender, EventArgs e)
        {
          int lineCount = SendMessage(txtNotes.Handle, EM_GETLINECOUNT, IntPtr.Zero, IntPtr.Zero);
          MessageBox.Show(lineCount.ToString());
          StringBuilder buffer = new StringBuilder(256);
          SendMessage_Ex(txtNotes.Handle, EM_GETLINE, lineCount - 1, buffer);
          MessageBox.Show(buffer.ToString());
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width