Home > OS >  How to convert RichTextBox.Selection.Start to int?
How to convert RichTextBox.Selection.Start to int?

Time:02-06

How to convert RichTextBox.Selection.Start to int?

In winforms, you can do this :

int i = richTextBox.SelectionStart;

How to do the same in WPF?

All I need is the int value of the start position of the selection.

I tried to find information on this issue, but all was in vain. There are quite a few methods in RichTextBox.Selection.Start in wpf, but none seem to give the desired result. It is very strange that so many difficulties arose with such a simple task.

CodePudding user response:

Get both the start of the selection and the start of the document as a TextPointer. You can then use the GetOffsetToPosition method to calculate the distance between the two in text symbols.

TextPointer selStart = richTextBox.Selection.Start;
TextPointer docStart = richTextBox.Document.ContentStart;
int offset = docStart.GetOffsetToPosition(selStart);

CodePudding user response:

I would recommend to use Databindings and MVVM. You shouldn't call your User Interface directly. Its better for maintainability.

  • Related