我想在 wpf richtextbox 中获得第一个字符索引 [英] i want to getting first character index at line, in wpf richtextbox

查看:29
本文介绍了我想在 wpf richtextbox 中获得第一个字符索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入行号时,我想知道该行第一个字符的索引.

When I entered the linenumber I would like to know the index of the first character of the line.

每次点击按钮在richtextbox1 上打印文本.

Each time click the button print text on richtextbox1.

我想知道输出行第一个字符的索引.

and I would like to know the index of the first character of the output line.

private int GetTextPositionAndLength(int position, int lineIndex, out int length)
{
    int richtTextLineIndex = GetFirstCharIndexFromLine(lineIndex);
    int index = 0;
    length = 0;
    return index + richtTextLineIndex;
}

private int GetFirstCharIndexFromLine(int lineIndex)
{
    // What should I enter the code?
    int index = 0;
    return index;
}

帮帮我

推荐答案

    private int GetFirstCharIndexFromLine(int lineIndex)
    {
        int index = 0;

        var rtb = yourRichTextBox;

        TextRange textRange = new TextRange(
            rtb.Document.ContentStart,
            rtb.Document.ContentEnd
        );

        var alltext = textRange.Text;

        string[] lines = alltext.Replace("\n", "").Split('\r');

        if (lineIndex > lines.Count())
            throw new ArgumentOutOfRangeException("lineIndex");

        for (int i = 0; i < lineIndex; i++)
        {
            index += lines[i].Length;
        }

        return index;
    }

希望有帮助

这篇关于我想在 wpf richtextbox 中获得第一个字符索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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