|
-
February 24th, 2003, 06:23 PM
#1
Problem with RichTextBox
I'm writing a text editor using RTB (RichTextBox), and I would like to display the line and the offset of the caret on the status bar.
I can grab the line number using
RTB.GetLineFromCharIndex(RTB.SelectedIndex);
but I can't find how to find the offset in the line.
Any has a solution?
-
February 24th, 2003, 06:42 PM
#2
check out this code
private void GoToLineAndColumn(RichTextBox RTB, int Line, int Column)
{
int offset = 0;
for (int i = 0; i < Line - 1 && i < RTB.Lines.Length; i++)
{
offset += RTB.Lines[i].Length + 1;
}
RTB.Focus();
RTB.Select(offset + Column, 0);
}
Paresh
-
February 24th, 2003, 07:14 PM
#3
Get Line & Column RichTextBox
Looks good. Thanks.
Here's my final solution, in case anyone in the future will need it.
At the end of the following code, iLine contains the caret's line number and iCol contains the column.
int iLine = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart) + 1;
int iOffset = 0;
for (int i = 0; i < iLine - 1; ++i)
{
iOffset += richTextBox1.Lines[i].Length + 1;
}
int iCol = richTextBox1.SelectionStart - iOffset;
-
February 24th, 2003, 07:17 PM
#4
yes, that looks nice and neat code.
may be it can be used somewhere...
good work kep it up
-
March 2nd, 2005, 02:50 AM
#5
Re: Problem with RichTextBox
WOW Thankyou I have been trying to figure out how to do this for three days lol I finally realized how to spell caret lol and boom here it is thankyou thankyou thankyou p.s. I slightly changed the code as I wanted to mimic notepad and the col was off by 1 so your code said that the col is 0 when it actually is 1 below is my code 
P.S. Thankyou again 
int iLine = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart) + 1;
int iOffset = 0;
for (int i = 0; i < iLine - 1; ++i)
{
iOffset += richTextBox1.Lines[i].Length + 1;
}
int iCol = (richTextBox1.SelectionStart - iOffset) + 1;
this.statusBarPanel2.Text = "Ln " + iLine + ", Col " + iCol;
oh did I say thankyou 
I found this link but it was way to much code and yes this code is nice and neat 
http://www.codeguru.com/Csharp/Cshar...int.php/c8145/
and my link to my post on this site was here 
http://www.codeguru.com/forum/showth...69#post1105669
Thankyou again
I do have one small problem I put it into the _MouseDown and the _KeyPress works great except when I use the arrow keys and the delete key ext... is there one spot that I can put it where it will work whether or not its a mouse click or a key pressed (All keys relevent including arrow keys ext...)
Help
Last edited by Crash1hd; March 2nd, 2005 at 03:07 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|