471,330 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

RichTextBox.GetLineFromCharIndex discrepancy

Hi,

Im having problems with the value being returned by the richtextbox.GetLineFromCharIndex method. Its returning a value that is more than the richtextbox.Lines.Length.

Im trying to print selected lines of text from a richtextbox control.

Here is my sample code:

The lines of text in the richtextbox control is 3283.

int startPos = richTextCtrl.SelectionStart;

printLineIndex = richTextCtrl.GetLineFromCharIndex(startPos);

// Print each line of the file.
while ( count < linesPerPage && printLineIndex < printLineLimit )
{
line = richTextCtrl.Lines[printLineIndex];
..//
I'm getting values that are greater than 3283; thus an exception is being thrown.
..
..
..
}

Can I implement it in a way that I'm not going to use the GetLineLineFromCharIndex method?

Thanks,

Mark Caliston
Jul 8 '08 #1
2 4059
On Tue, 08 Jul 2008 00:49:15 -0700, macky <calistonwrote:
Im having problems with the value being returned by the
richtextbox.GetLineFromCharIndex method. Its returning a value that is
more than the richtextbox.Lines.Length.
The GetLineFromCharIndex() method returns a line number based on the
control's _display_, not the logical lines (that is, the lines based
solely on line-breaks in the text). If your control has lines wrapped due
to simply not fitting, they will add to the number of lines considered for
the purposes of GetLineFromCharIndex().

If you don't want your printing to be done to match how the text is
displayed on the screen, I suggest you just use the SelectedText property,
and format the text in that string according to your needs. You can use
String.Split() to handle the line-breaks, to do effectively the same thing
that the Lines property was doing (but in this case, in a way that can be
correlated to the text you're printing). Or you could just scan the
string as you print it, looking for line-breaks.

Alternatively, you could just set the WordWrap property to false so that
the displayed lines matches the logical lines.

Off the top of my head, I don't recall anything in the text box classes
that would allow you to directly correlate the character indices to
logical lines when the control is wrapping lines. You could do it
yourself, of course, by simply building your own mapping (enumerating the
array of strings returned by Lines, counting the number of characters in
each line). But that seems like a lot of trouble.

Pete
Jul 8 '08 #2
Thanks Pete.

I just used the SelectedText property, and split the strings.

Mac
Jul 11 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Rachel Suddeth | last post: by
3 posts views Thread by GSX | last post: by
2 posts views Thread by Mark Raishbrook | last post: by
2 posts views Thread by Don Gollahon | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.