я хочу получить индекс первого символа в строке, в wpf richtextbox

Когда я ввел номер строки, я хотел бы знать индекс первого символа строки.

Каждый раз нажимайте кнопку печати текста на richtextbox1.

и я хотел бы знать индекс первого символа выходной строки.

это мой код:

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

Помоги мне


person Kim Sang Hoon    schedule 16.08.2015    source источник


Ответы (1)


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

Надеюсь, поможет

person VMaleev    schedule 16.08.2015