2

How can i know If user click Enter in keyboard in RichEditBox?

this code does not work

  private void Editor_KeyDown(object sender, KeyRoutedEventArgs e)
  {
      var dia = new MessageDialog(e.Key + "");
      dia.ShowAsync();
  }

also This code does not work

 private void Editor_KeyDown(object sender, KeyRoutedEventArgs e)
 {
     if (e.OriginalKey == (VirtualKey)(char)13)
     {
         NumberEditor.Text += Convert.ToString(_LineNum) + Environment.NewLine;
         ++_LineNum;
     }
 }

How can i get line of row in RichEditBox and how can i change text from code in RichEditBox?

I want to make an Editor and any help is appreciated.

Regards

AVK
  • 3,893
  • 1
  • 22
  • 31
Qais Bsharat
  • 146
  • 3
  • 12

1 Answers1

0

Look this method:

   private void ChangeLine()
    {
        var textRange = MyRichEditBox.Document.GetRange(MyRichEditBox.Document.Selection.StartPosition, MyRichEditBox.Document.Selection.StartPosition);
        textRange.Expand(TextRangeUnit.Line);

        //Change line size.
        textRange.CharacterFormat.Size = 30;

        //Center the paragraph
        textRange.ParagraphFormat.Alignment = ParagraphAlignment.Center;

        //this will change text of the range
        textRange.Text = "My new text";
    }

From Document you have to get ITextRange. After that you can expand it to TextRangeUnit.Line and get whole line. Now you can change the style and text of the line.

Try this code for KeyDownEvent:

 public sealed partial class TextEditPage : Page
{
    private readonly KeyEventHandler _keyDownHandler;

    //Constructor
    public TextEditPage()
    {
        this.InitializeComponent();
        this.Unloaded += OnUnloaded;

        //Add keydown event
        this._keyDownHandler = OnKeyDown;
        RtfBox.AddHandler(KeyDownEvent, this._keyDownHandler, true);
    }

    private void OnKeyDown(KeyRoutedEventArgs e)
    {
        //enter your code here
    }

    private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
    {
        this.RemoveHandler(KeyDownEvent, _keyDownHandler);
    }
}

Don't forget to remove handler in Unloaded.

DobriBala
  • 132
  • 7