RichTextBox 和插入符号位置 [英] RichTextBox and Inserting at Caret Positions

查看:49
本文介绍了RichTextBox 和插入符号位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事情是这样的:我有一个 RichTextBox 控件,它工作正常.问题是有一个按钮插入当前日期时间"将当前日期时间添加/注入到 RichTextBox 中.用户可以在插入符号指向的任何地方输入日期时间.这涉及复杂的字符串操作和东西.

Here is the deal: I have a RichTextBox control and it works fine. The problem is that there is a button "Insert Current DateTime" which adds/injects the current datetime into the RichTextBox. The user can enter the datetime anywhere where the caret is pointing. This involves complicated string manipulation and stuff.

关于如何获得当前插入符号位置的任何想法.每当我得到 RichTextBox.CaretPositon 时,它似乎都指向 RichTextBox 的开头,而不是实际插入符号所在的位置.

Any ideas how to get the current caret position. Whenever I get RichTextBox.CaretPositon it seems it is pointing to the start of the RichTextBox and not where the actual caret is.

更新 1:

日期时间按钮点击代码:

The date time button click code:

 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"); 
                }
            }
        }

更新 2:

原来问题是日期时间按钮是可聚焦的.我把它变成了不可聚焦,它按预期工作.当焦点在 RichTextBox 上丢失时,它正在重置插入符号位置.它只发生过一次,因为在代码中 btn_DateTime 被动态设置为 Focusable = false.我在 XAML 中放置了 Focusable = false 并且从一开始就一切正常.

Turned out the problem was with the DateTime button being Focusable. I turned it to be not focusable and it worked as expected. When focus was lost on the RichTextBox it was resetting the caret position. It happened only once since in the code the btn_DateTime was dynamically being set as Focusable = false. I placed Focusable = false in XAML and everything worked fine from the start.

推荐答案

我正在使用此代码成功完成您正在尝试的操作:

I'm using this code to successfully do what you are attempting:

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 是导致问题的原因...

It looks like SetValue is changing the text so based on my test that actually changing the text resets the caret, I would agree with you that SetValue is causing the problem...

这篇关于RichTextBox 和插入符号位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆