开发者

How to get current word at the Caret position from a String(TextBox) which is having SPACE and ENTER key?

开发者 https://www.devze.com 2023-02-18 09:09 出处:网络
How to get the current word at the Cursor position from a TextBox which will contain \" \" and \"\\r\\n\" as the Word separ开发者_StackOverflow中文版ator?Something similar to this should work:

How to get the current word at the Cursor position from a TextBox which will contain " " and "\r\n" as the Word separ开发者_StackOverflow中文版ator?


Something similar to this should work:

var currentWord = textBox.Text.Substring(textBox.SelectionStart, textBox.Text.IndexOf(" ", textBox.SelectionStart));


I use this to select a word:

    private void selectWord()
    {
        int cursorPosition = textBox1.SelectionStart;
        int nextSpace = textBox1.Text.IndexOf(' ', cursorPosition);
        int selectionStart = 0;
        string trimmedString = string.Empty;
        if (nextSpace != -1)
        {
            trimmedString = textBox1.Text.Substring(0, nextSpace);
        }
        else
        {
            trimmedString = textBox1.Text;
        }


        if (trimmedString.LastIndexOf(' ') != -1)
        {
            selectionStart = 1 + trimmedString.LastIndexOf(' ');
            trimmedString = trimmedString.Substring(1 + trimmedString.LastIndexOf(' '));
        }

        textBox1.SelectionStart = selectionStart;
        textBox1.SelectionLength = trimmedString.Length;
    }

and this to get the selected word:

textBox1.SelectedText


I have this issue now and here is my approach to deal with it. TxtLineCodes is the RichTextBox control, Please see my comments inside the code.

private void CheckCurrentWord()
{
    // current caret position
    var currentposition = TxtLineCodes.SelectionStart;
    // get line number
    var linenumber = TxtLineCodes.GetLineFromCharIndex(currentposition);
    // get the first character index of the line
    var firstlineindex = currentposition;
    if (linenumber == 0)
    {
        firstlineindex = 0;
    }
    else
    {
        while (TxtLineCodes.GetLineFromCharIndex(firstlineindex) == linenumber)
        {
            firstlineindex--;
        }
        //fix the last iteration
        firstlineindex += 1;
    }

    // if caret is not in the end of the word discover it
    var lastcaretwordindex = currentposition;
    if (lastcaretwordindex < TxtLineCodes.Text.Length)
        while (lastcaretwordindex < TxtLineCodes.Text.Length && TxtLineCodes.Text.Substring(lastcaretwordindex, 1) != " ")
        {
            lastcaretwordindex += 1;
        }

    // get the text of the line (until the cursor position)
    var linetext = TxtLineCodes.Text.Substring(firstlineindex, lastcaretwordindex - firstlineindex);
    // split all the words in current line
    string[] words = linetext.Split(' ');
    // the last word must be the current word
   System.Diagnostics.Debug.WriteLine("current word: " + words[words.Length - 1]);
    // and you can also get the substring indexes of the current word
    var currentwordbysubstring = TxtLineCodes.Text.Substring(lastcaretwordindex - words[words.Length - 1].Length, words[words.Length - 1].Length);

    var startindex = lastcaretwordindex - words[words.Length - 1].Length;
    var lastindex = startindex + words[words.Length - 1].Length-1;
    System.Diagnostics.Debug.WriteLine("current word: " + currentwordbysubstring + " and its in index (" + startindex + "," + lastindex + ")");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消