Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is ScrollToCaret in a TextBox in WPF?

Tags:

c#

wpf

textbox

I'm unable to find that function. Basically I have a multiline text box and when I perform a search, I highlight the result. But if the result is not within view, I would have to manually scroll down until I find the highlighted result, which beats the purpose of the "Find" functionality.

I don't want to use RichTextBox because I've encountered some performance issues with it.

like image 548
Kingamoon Avatar asked Nov 29 '12 08:11

Kingamoon


1 Answers

You could use GetLineIndexFromCharacterIndex in combination with ScrollToLine:

var selectionStart = x;
var selectionLength = y;
textBox.Select(selectionStart, selectionLength);
textBox.ScrollToLine(textBox.GetLineIndexFromCharacterIndex(textBox.SelectionStart));
like image 90
Daniel Hilgarth Avatar answered Oct 29 '22 20:10

Daniel Hilgarth