I'm porting an application from WinForms to WPF and I've hit a snag while trying to get the line and column number for where the selection is in the text box. I was able to do it quite simply in WinForms but WPF has a completely different way of implementing a RichTextBox so I have no idea how to go about it.
Here is my WinForms solution
int line = richTextBox.GetLineFromCharIndex(TextBox.SelectionStart);
int column = richTextBox.SelectionStart - TextBox.GetFirstCharIndexFromLine(line);
LineColumnLabel.Text = "Line " + (line + 1) + ", Column " + (column + 1);
This won't work with WPF because you can't get the index of the current selection.
HERE IS THE WORKING SOLUTION:
int lineNumber;
textBox.CaretPosition.GetLineStartPosition(-int.MaxValue, out lineNumber);
int columnNumber = richTextBox.CaretPosition.GetLineStartposition(0).GetOffsetToPosition(richTextBox.CaretPosition);
if (lineNumber == 0)
columnNumber--;
statusBarLineColumn.Content = string.Format("Line {0}, Column {1}", -lineNumber + 1, columnNumber + 1);