WPF-从行中获取最后一个字符索引

问题描述 投票:0回答:1

WPF TextBox控件具有方法“ Get First字符Index FromLine(int line_index)”。我需要C#Get Last字符Index FromLine(int line_index)方法。

我的最终目标是在单击该行时选择整个行。我在这里查看了其他类似问题,但没有一个对我有用。

这也将帮助我实现我需要的另一个功能,即将光标移动到指定行的末尾。

c# wpf textbox
1个回答
0
投票

我有一个非常简单的Nuget软件包:

DataJuggler.Core.UltimateHelper

using DataJuggler.Core.UltimateHelper;
using DataJuggler.Core.UltimateHelper.Objects;

public char GetLastChar(string inputText, int lineIndex)
{
    // initial value
    string temp = " ";
    char lastChar = temp[0];

    // if the inputText string exists and lineIndex is not below zero
    if ((TextHelper.Exists(inputText)) && (lineIndex >= 0))
    {
        // get hte inputText
        List<TextLine> lines = WordParser.GetTextLines(inputText);

        // verify the index is in range
        if ((ListHelper.HasXOrMoreItems(lines, index)) && (index < lines.Count))
        {
            // Get the textLine
            TextLine textLine = lines[lineIndex];

            // verify there is text
            if (TextHelper.Exists(textLine.Text))
            {
               // Set the return value
               lastChar = textLine.Text[textLine.Text.Length -1];
            }
        }
    }

    // return value
    return lastChar;
}

如果您不想使用Nuget包,则完整的源代码在这里:

https://github.com/DataJuggler/UltimateHelper

© www.soinside.com 2019 - 2024. All rights reserved.