# [.net] How do I get the current line number and cursor position in a RichTextBox?

This topic is 4453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've worked out how to select text, change it and edit which all bodes well for my IDE, but I now need to determine which line the user is currently editing as well as the cursor position. I.e the exact character he/she is editing for my intellisense.Also need to determine the character the line starts at. I'm looking for something like,

int LineNum = myRichTextBox.CurrentLine;
int StartChar = myRichTextBox.LineStart( LineNum );
int EndChar = StartChar + myRichTextBox.LineLength( LineNum );
String txt = myRichTextBox.Text;
txt = txt.substring( startchar,endchar );


So in total I need currentline, linestart and linelength. How would I go abiout these? It must be possible otherwise I doubt any ides would even better possible. Or are there better ways of doing this I'm just not seeing? Is this is a good idea btw, I plan to tokenize the currentline, and base the intellisense on this using a listbox created over the richtextbox? It works in my tests, displays fine but how do they do it in VC# Express' ide? That's what I want to emulate mostly.

##### Share on other sites
This should give you what you need.

http://www.codeproject.com/cs/miscctrl/RicherRichTextBox.asp

theTroll

##### Share on other sites
OK with that I can get the current line, and the current character position.

I still need a way to determine at which character the line starts at, and which one it ends at.

I'm surprised that I need to use that site you showed me though, I thought this being net it would have all these features built in. I mean how did they manage the VC# ide without them?

##### Share on other sites
OK with that I can get the current line, and the current character position.

I still need a way to determine at which character the line starts at, and which one it ends at.

I'm surprised that I need to use that site you showed me though, I thought this being net it would have all these features built in. I mean how did they manage the VC# ide without them?

##### Share on other sites
The standard RichTextBox gives you SelectionStart, SelectionLength, GetLineFromCharIndex and Lines, so your little example could be:
int LineNum = myRichTextBox.GetLineFromCharIndex(myRichTextBox.SelectionStart);string txt = myRichTextBox.Lines[LineNum];

##### Share on other sites
That works, thanks.

I need just one more thing now, and that is a way to determine which character the cursor is at the current line rather than where it is globally.

Possible?

##### Share on other sites
Hmm, that depends on exactly what you mean by that. The character index (SelectionStart) is the offset from the start of the content, so I guess you could loop through all the previous lines and add up the lengths, then subtract that from the global index which would give you the index on that line.

Alternatively you could use GetPointFromCharIndex, which gives you the X,Y of the selection, and calculate how many chars into the line that X position is ... don't know if you can do that reliably though.

Oh, and on the previous code, watch out for this:
Quote:
 The GetLineFromCharIndex method returns the physical line number where the indexed character is located within the control. For example, if a portion of the first logical line of text in the control wraps to the next line, the GetLineFromCharIndex method returns 1 if the character at the specified character index has wrapped to the second physical line.

... if you're using word-wrap, you're kind of buggered.

##### Share on other sites
Ok thanks I think I get what you mean, could you write a small example
so I can be sure I'm on the right track? :)

##### Share on other sites
I think this dull looping code should do the trick:
// As aboveint ss = richEdit.SelectionStart;int line = richEdit.myRichTextBox.GetLineFromCharIndex(ss);// Find what index the start of that line isint sofar = 0;for(int i = 0; i < line; i++) sofar += richEdit.Lines.Length;int index_in_line = ss - sofar;

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 32
• 41
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631742
• Total Posts
3001989
×