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!
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.
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:
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!
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.
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!
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:
Dim previousLineIndex As Integer = Me.RichTextBox1.GetLineFromCharIndex(Me.RichTextBox1.SelectionStart) - 1
Dim lastCharIndex As Integer = Me.RichTextBox1.SelectionStart - 1
While Me.RichTextBox1.GetLineFromCharIndex(Me.RichTextBox1.SelectionStart) > previousLineIndex
lastCharIndex -= 1
End While
Dim firstCharIndex As Integer = lastCharIndex
While Me.RichTextBox1.GetLineFromCharIndex(firstCharIndex - 1) = previousLineIndex
firstCharIndex -= 1
End While
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.
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!
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?
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!