I want to move the content of a RichTextBox to a specific line of the content. The RichTextBox provides the method ScrollToVerticalOffset from the embedded ScrollViewer. The method is documented in MSDN, but the measurement unit of the parameter named offset is not specified. The Type is double.
A VerticalOffset property of a TextBox is documented as in device-independent units (1/96th inch per unit).
So I tried to calculate the offset from the font size. The font size is given in pixels. The resulting formula is
offset = fontSize * 96 / 72 * lineNumber;
But this jumps way behind the desired line. For now I am using this calculation:
offset = fontSize * lineNumber;
Is this correct?
It is true that long time passes from asking this question, but still have not found the right answer to it !!
I used this code now, that's good for me really:
var offset = (lineNumber * (fontSize + 2)) - richTextBox.ActualHeight / 2;
richTextBox.ScrollToVerticalOffset(offset);
If you know one solution better than this way, please help me.
TextPointer myTextPointer1 = Paragraph.ContentStart.GetPositionAtOffset(20);
TextPointer myTextPointer2 = Paragraph.ContentEnd.GetPositionAtOffset(-10);
RichTextBox.Selection.Select(myTextPointer1, myTextPointer2);
DependencyObject currObj = RichTextBox.CaretPosition.Parent;
FrameworkElement fe = currObj as FrameworkElement;
if (fe != null)
{
fe.BringIntoView();
}
else
{
FrameworkContentElement fce = currObj as FrameworkContentElement;
if (fce != null)
{
fce.BringIntoView();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With