473,231 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 4157
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: DMD | last post by:
How do I determine the column associated with the current edit location within a RichTextBox? The following code allow me to determine the current line txtLineNumber.Text =...
1
by: Arlyn_L | last post by:
Can someone please explain why I get a different result when using the GetLineFromCharIndex method on the last char in the Text property and the Lines.Length property? MainForm_gedcomTextBox is...
6
by: Rachel Suddeth | last post by:
I have the index of a line in the Lines array of a RichTextBox. I would like to have it scroll so that line displays at the top. Is there no way to do this? The only way I can see to make it...
3
by: GSX | last post by:
I've been reading and searching (and searching and reading) for far too many hours. I'm rather new - so I apologize if this is an over asked question... I'm working on a small text editor as a...
0
by: Sakharam Phapale | last post by:
Hi All, How to get the starting and end character position of the given line no in RichTextBox? I have written following code but it takes more time. Is there any RichTextBox function or API...
2
by: Mark Raishbrook | last post by:
....without resorting to SendMessage. Imagine, for example, I have a small RTB whose text has wrapped as follows: "This is a small test to see if I can get" "both of these lines". ...
1
by: Udi | last post by:
Hi, I have derived my ConsoleTextBox from RichTextBox. I'm trying to implement a console-like text box with history capabilities. My problem is that when I'm trying to append a command, I can't...
0
by: Udi | last post by:
Hi, I have derived my ConsoleTextBox from RichTextBox. I'm trying to implement a console-like text box with history capabilities. My problem is that when I'm trying to append a command, I can't...
2
by: Don Gollahon | last post by:
..NET 1.1 How do I get the line # the cursor is currently in in a RichTextBox? Cursor x, y coordinates in the component would be nice. Thanks. -- Don Gollahon
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.