سؤال

I have a RichTextBox that i am searching text in and I want to be able to control what the enter key does when text is selected. I am able to use this if test below to call the method that I want, but my issue is after the method gets hit when the enter key is pressed it then moves the text to the second line and I want to be able to stop this from happening when the text is highlighted.

I test to check if the text is selected when enter is pressed.

if (IsTextSelected == true)
{
   btnSearch_Click(sender, null);
}
هل كانت مفيدة؟

المحلول

You can listen to the PreviewKeyDown event like:

  <RichTextBox PreviewKeyDown="RichTextBox_PreviewKeyDown"/>

and in the handler:

private void RichTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
       // DO YOUR WORK HERE and then set e.Handled to true on condition if you want to stop going to next line//

        e.Handled = true;
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top