1 //Execution Events2 Private voidButton_Click (Objectsender, RoutedEventArgs e)3 {4 5 if(Txtstr. Text = ="")6 {7MessageBox.Show ("the query string is empty! ");8 return;9 }Ten cxtxt_string (); One } A - //Execution Method - intindex =0;//Search starting from the No. 0 string the Public voidcxtxt_string () - { - stringstr = TextBox1.Text;//set the string you want to find -index = textBox2.Text.IndexOf (str, index);//returns the index position of str in TextBox1 + if(Index <0)//If no string is found, the return - { +index =0; ATextbox2.selectionstart =0; atTextbox2.selectionlength =0; -MessageBox.Show ("has reached the end"); - return; - } -Textbox2.selectionstart = index;//set where you want the selected string to start -Textbox2.selectionlength = str. Length;//set the end position of the string to be selected inindex++; -Textbox2.focus ();//Set Focus to + //int rows = Textbox2.getfirstvisiblelineindex ();//get the row index of the first character in a visible text box - intline = Textbox2.getlineindexfromcharacterindex (index);//returns the line number where the specified string index is located the //Debug.WriteLine (Rows + ",," + line); * intLastLine =Textbox2.getlastvisiblelineindex (); $ if(Line >lastline)Panax Notoginseng { -Textbox2.scrolltoline (lastline+1);//Scroll to the specified row index in the view the } + } A } the}
WPF finds strings in a TextBox and scrolls automatically to the line where the string is located
WPF TextBox string Search, scrolling to the current line