Finding Char Index in current line
-
this code shows me the current line and character index in a richtextbox control. the char index, however, is counted from position 0 at line 0 but i need the char index to start from 0 at each line and NOT from line 0. how can i do this?
private void rtb1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { //Shows the current Line of selection int Line = rtb1.GetLineFromCharIndex(rtb1.SelectionStart); MessageBox.Show(Line.ToString()); //Shows the Character Index of the selection int pos = rtb1.GetCharIndexFromPosition(new Point(e.X, e.Y)); MessageBox.Show(pos.ToString()); }
Thanks for your help. .gonad -
this code shows me the current line and character index in a richtextbox control. the char index, however, is counted from position 0 at line 0 but i need the char index to start from 0 at each line and NOT from line 0. how can i do this?
private void rtb1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { //Shows the current Line of selection int Line = rtb1.GetLineFromCharIndex(rtb1.SelectionStart); MessageBox.Show(Line.ToString()); //Shows the Character Index of the selection int pos = rtb1.GetCharIndexFromPosition(new Point(e.X, e.Y)); MessageBox.Show(pos.ToString()); }
Thanks for your help. .gonadthe only way I might see is if you do this: int Line = rtb1.GetLineFromCharIndex(rtb1.SelectionStart); int pos = rtb1.SelectionStart; while ( (newLine = rtb1.GetLineFromCharIndex(pos--) == Line) MessageBox.Show("The beginning char index of the line is " + ++pos.ToString()); now that code is not tested I just typed it in but it should work for the char index of the first Line MessageBox.Show(String.Format("Your caret is {0} characters away from the beginning",rtf1.SelectionStart - pos); I'm not an expert yet, but I play one at work. Yeah and here too.