(Rich) Text Box and cursor position
-
Hi all. I've searched through the forums but couldn't find an answer. I have a rich text box (but the question stands even for "simple" text boxes), and I would like to get the cursor's position within it. Something like "line X column Y", or even just "char Z". Any suggestions? In particular, I'm trying to get to this info from the handler of a mouse click event on the rich text box --just in case the System.Windows.Forms.MouseEventArgs parameter can help Thanks in advance, F.O.R.
-
Hi all. I've searched through the forums but couldn't find an answer. I have a rich text box (but the question stands even for "simple" text boxes), and I would like to get the cursor's position within it. Something like "line X column Y", or even just "char Z". Any suggestions? In particular, I'm trying to get to this info from the handler of a mouse click event on the rich text box --just in case the System.Windows.Forms.MouseEventArgs parameter can help Thanks in advance, F.O.R.
Well, so far here's what I found... if anyone has any better idea, feel free to post :-)
//In the handler for the mouse-click on the rich text box... //where e is the System.Windows.Forms.MouseEventArgs parameter... int ndex = this.rBox_MarkedUp.GetCharIndexFromPosition( new System.Drawing.Point(e.X, e.Y)); int line = this.rBox_MarkedUp.GetLineFromCharIndex(ndex);
ndex is now the 0-based index of the char clicked and line is the number of the line (0-based as well). We now return to the usual stream of questions :-) Thanks, F.O.R. -
Well, so far here's what I found... if anyone has any better idea, feel free to post :-)
//In the handler for the mouse-click on the rich text box... //where e is the System.Windows.Forms.MouseEventArgs parameter... int ndex = this.rBox_MarkedUp.GetCharIndexFromPosition( new System.Drawing.Point(e.X, e.Y)); int line = this.rBox_MarkedUp.GetLineFromCharIndex(ndex);
ndex is now the 0-based index of the char clicked and line is the number of the line (0-based as well). We now return to the usual stream of questions :-) Thanks, F.O.R.Hi. I just went thru this as well, but this is what i worked out. it's basically the same thing you already figured out.
private void rtb1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { leftMouse(e.X, e.Y); } private void leftMouse(int X, int Y) { posStart = rtb1.GetCharIndexFromPosition(new Point(X, Y)); }