473,805 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RichTextBox.Get LineFromCharInd ex discrepancy

Hi,

Im having problems with the value being returned by the richtextbox.Get LineFromCharInd ex method. Its returning a value that is more than the richtextbox.Lin es.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.Se lectionStart;

printLineIndex = richTextCtrl.Ge tLineFromCharIn dex(startPos);

// Print each line of the file.
while ( count < linesPerPage && printLineIndex < printLineLimit )
{
line = richTextCtrl.Li nes[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 GetLineLineFrom CharIndex method?

Thanks,

Mark Caliston
Jul 8 '08 #1
2 4197
On Tue, 08 Jul 2008 00:49:15 -0700, macky <calistonwrot e:
Im having problems with the value being returned by the
richtextbox.Get LineFromCharInd ex method. Its returning a value that is
more than the richtextbox.Lin es.Length.
The GetLineFromChar Index() 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 GetLineFromChar Index().

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
1810
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 = FormatNumber(RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart())+1,0 How do I determine the column location within the line? I must be missing something obvious, any help would be appreciated
1
2871
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 a RichTextBox. For example this code: private void End_menuItem_Click(object sender, System.EventArgs e) {
6
29232
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 scroll is with ScrollToCaret(). But I don't know how to set the "caret" to a particular line. Any suggestions? -Rachel
3
9174
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 learning project using the RichTextBox control. I wish to display the current line and total line count on the status bar, and also the cursor position on the current line. I have found the current line by using...
0
1522
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 function which gives that? Private Sub SetLineStartEndPosition(ByVal myRichTextBox as RichTextBox, ByVal LineNo As Integer, ByRef StartPosition As Integer, ByRef EndPosition As Integer)
2
1599
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". GetLineFromCharIndex(RTB.TextLength) returns 1, telling me there are two lines in there. Using the Lines property to retrieve them will fail, however. As the line has soft-wrapped (i.e. the user has not hit Enter after "get"),
1
3411
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 place the cursor at the end of the text. ### See SetCmdLn() method: #### protected override bool IsInputKey(Keys keyData)
0
1589
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 place the cursor at the end of the text. ### See code below - problem in SetCmdLn() #### My questions are:
2
2919
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
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.