Current line and column numbers in a RichTextBox in a Winforms application

后端 未结 3 1595
一整个雨季
一整个雨季 2020-12-19 11:24

How do I get the current line and column numbers in a RichTextBox in a Winforms application?

NOTE

Folks, I just want a simple solution, if i

3条回答
  •  清歌不尽
    2020-12-19 11:59

    Thought I'd post a slightly simpler way of doing it.

    // Get the line.
    int index = richTextBox.SelectionStart;
    int line = richTextBox.GetLineFromCharIndex(index);
    
    // Get the column.
    int firstChar = richTextBox.GetFirstCharIndexFromLine(line);
    int column = index - firstChar;
    

    Get the current selected index, get the current line, then to get the column subtract the selected index from the index of the first character of that line.

提交回复
热议问题