RichTextBox and insert in Caret position

Here's the deal: I have a RichTextBox control and it works great. The problem is that there is a "Insert Current Date" button that adds / enters the current time and time into the RichTextBox. The user can enter the date-time at any place where the carriage indicates. This is due to complex string manipulation and other materials.

Any ideas on how to get the current caret position. Whenever I get a RichTextBox.CaretPositon, it seems to point to the beginning of the RichTextBox, not the actual caret.

UPDATE 1:

Date Key Button Code Key:

 private void DateTimeStampButton_Click(object sender, RoutedEventArgs e)
        {
            //TextRange tr = new TextRange(textBox.Selection.Start, textBox.Selection.End);
            var tr = new TextRange(textBox.Document.ContentStart, textBox.Document.ContentEnd);

            if(tr.Text.Length == 2)
            {
                if(tr.Text == "\r\n")
                {
                    tr.Text = tr.Text.TrimStart(new[] { '\r', '\n' }); 
                }
            }

            textBox.CaretPosition.InsertTextInRun(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":  ");

            DateTimeStampButton.Focusable = false;
        }

 private void SharpRichTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            SetValue(TextProperty, Text);

            var binding = BindingOperations.GetBinding(this, TextProperty);

            if (binding == null) return;

            if (binding.UpdateSourceTrigger == UpdateSourceTrigger.Default || binding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus)
            {
                // if (TextProperty != null) BindingOperations.GetBindingExpression(this, TextProperty).UpdateSource();
            }
        }






public string Text
        {
            get
            {
                var newValue = new TextRange(Document.ContentStart, Document.ContentEnd).Text.RemoveNewLineAndReturn(); 
                return newValue; 
            }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    SetValue(TextProperty, value.RemoveNewLineAndReturn());
                    Document.Blocks.Clear(); 
                    Document.Blocks.Add(new Paragraph(new Run(value))); 
                    OnPropertyChanged("Text"); 
                }
            }
        }

UPDATE 2:

, , DateTime Focusable. , , , . RichTextBox, . , btn_DateTime Focusable = false. Focusable = false XAML, .

+5
3

, , :

private void insertNowButton_Click(object sender, RoutedEventArgs e)
{
    //NOTE:  The caret position does not change.
    richTextBox1.CaretPosition.InsertTextInRun(DateTime.Now.ToString());
}

: 1

private void DateTimeStampButton_Click(object sender, RoutedEventArgs e)
{
    var tr = new TextRange(textBox.Document.ContentStart, textBox.Document.ContentEnd);

    if (tr.Text.Length == 2)
    {
        if (tr.Text == "\r\n")
        {
            tr.Text = tr.Text.TrimStart(new[] { '\r', '\n' });
        }
    }

    /* Changing the text is the only way I can get the date to insert at the beginning */
    tr.Text = "I need a beer at ";

    textBox.CaretPosition.InsertTextInRun(DateTime.Now.ToString());
}

, SetValue , , , , , SetValue ...

+9

WPFToolkit.Extended RichTextBox, . , , - .

, , , , RichTextBox.

, , , -

RichTextBox CaretPosition

CaretPosition RichTextBox.Selection.Start.InsertTextInRun( "SomeText" ).

, , .

, - :)

+3

This worked for me:

private void InsertText(String text, RichTextBox rtb)
{
    rtb.CaretPosition = rtb.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
    rtb.CaretPosition.InsertTextInRun(text);
}

I found the code here:

How to transfer a carriage to a certain number of positions in WPF RichTextBox?

+1
source

All Articles