By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

RichTextBox.GetLineFromCharIndex discrepancy

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.