473,796 Members | 2,703 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 4195
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
1809
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
9173
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
1521
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
1588
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
2917
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
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
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
10239
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
10190
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
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
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...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.