Center the view on a word you have searched in a textbox

2

I have 2 textbox, in one position and I show a text and in the other I enter a word, I look for it and I highlight it in the text. The texts are very long and not all, so that part of the text does not appear (use a scrollbar). I want to know if there is any way that if the word is not in the first visible lines go automatically to where the searched word is.
I use this to search and highlight:

      public void ResaltarTexto(TextBox txtBx, string texto) 
     {
        int posición = txtBx.Text.IndexOf(texto);                  
        txtBx.Focus();
        txtBx.SelectionStart = posición;
        txtBx.SelectionLength = texto.Length;

    }
    
asked by Xabier 22.11.2017 в 11:08
source

1 answer

3

Good Xavier,

You can use the property of TextBox ScrollToCaret(); that Scroll does to show you where you have the caret (selection) symbol:

 public void ResaltarTexto(TextBox txtBx, string texto) 
 {
    int posición = txtBx.Text.IndexOf(texto);                  
    txtBx.Focus();
    txtBx.SelectionStart = posición;
    txtBx.SelectionLength = texto.Length;
    txtBx.ScrollToCaret();
}
    
answered by 22.11.2017 / 11:23
source