none
WPF中RichTextBox如何设置选取点的开始位置和选择文本长度? RRS feed

  • 问题

  • 就是如何用代码选取RichTextBox中的文本,也就是类似以前的

    RichTextBox1.SelectionStart  =5;
    RichTextBox1.SelectionLength = 20;

    在wpf中要如何实现?

    2012年9月9日 6:03

答案

  • 请参考以下代码。

     public MainWindow()
            {
                InitializeComponent();
                int index = 5;
                int length = 20;
                TextPointer txStart = GetPointerFromCharOffset(index, RichText.Document.ContentStart, FlowDoc);
                TextPointer txEnd = GetPointerFromCharOffset(index + length, RichText.Document.ContentStart, FlowDoc);
                TextRange tr = new TextRange(txStart, txEnd);
                Console.WriteLine(tr.Text);
            }
            public TextPointer GetPointerFromCharOffset(Int32 charOffset, TextPointer startPointer, FlowDocument document)
            {
                TextPointer navigator = startPointer;
                if (charOffset == 0)
                {
                    return navigator;
                }
                TextPointer nextPointer = navigator;
                Int32 counter = 0;
                while (nextPointer != null && counter <= charOffset)
                {
                    if (nextPointer.CompareTo(document.ContentEnd) == 0)
                    {     
                        return nextPointer;
                    }
                    if (nextPointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                    {
                        nextPointer = nextPointer.GetNextInsertionPosition(LogicalDirection.Forward);
                        counter++;
                    }
                    else
                    {
                        nextPointer = nextPointer.GetNextInsertionPosition(LogicalDirection.Forward);
                    }
                }
                return nextPointer;
            }


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us

    2012年9月10日 6:55
    版主

全部回复

  • 请参考以下代码。

     public MainWindow()
            {
                InitializeComponent();
                int index = 5;
                int length = 20;
                TextPointer txStart = GetPointerFromCharOffset(index, RichText.Document.ContentStart, FlowDoc);
                TextPointer txEnd = GetPointerFromCharOffset(index + length, RichText.Document.ContentStart, FlowDoc);
                TextRange tr = new TextRange(txStart, txEnd);
                Console.WriteLine(tr.Text);
            }
            public TextPointer GetPointerFromCharOffset(Int32 charOffset, TextPointer startPointer, FlowDocument document)
            {
                TextPointer navigator = startPointer;
                if (charOffset == 0)
                {
                    return navigator;
                }
                TextPointer nextPointer = navigator;
                Int32 counter = 0;
                while (nextPointer != null && counter <= charOffset)
                {
                    if (nextPointer.CompareTo(document.ContentEnd) == 0)
                    {     
                        return nextPointer;
                    }
                    if (nextPointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                    {
                        nextPointer = nextPointer.GetNextInsertionPosition(LogicalDirection.Forward);
                        counter++;
                    }
                    else
                    {
                        nextPointer = nextPointer.GetNextInsertionPosition(LogicalDirection.Forward);
                    }
                }
                return nextPointer;
            }


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us

    2012年9月10日 6:55
    版主
  • 谢谢你,辛苦了!

    2012年9月10日 13:04