如何在C#中的TextBox中获取光标的列号?

时间:2023-01-01 13:34:03

I've got a multiline textBox that I would like to have a label on the form displaying the current line and column position of, as Visual Studio does.

我有一个多行文本框,我希望在窗体上有一个标签,显示当前行和列的位置,就像Visual Studio一样。

I know I can get the line # with GetLineFromCharIndex, but how can I get the column # on that line?

我知道我可以通过GetLineFromCharIndex得到#行,但是如何在该行上获得#列?

(I really want the Cursor Position on that line, not 'column', per se)

(我真的希望该行上的光标位置,而不是'列'本身)

3 个解决方案

#1


9  

int line = textbox.GetLineFromCharIndex(textbox.SelectionStart);
int column = textbox.SelectionStart - textbox.GetFirstCharIndexFromLine(line);

#2


2  

textBox.SelectionStart -
textBox.GetFirstCharIndexFromLine(textBox.GetLineFromCharIndex(textBox.SelectionStart))

#3


0  

Off the top of my head, I think you want the SelectionStart property.

在我的头脑中,我想你想要SelectionStart属性。

#1


9  

int line = textbox.GetLineFromCharIndex(textbox.SelectionStart);
int column = textbox.SelectionStart - textbox.GetFirstCharIndexFromLine(line);

#2


2  

textBox.SelectionStart -
textBox.GetFirstCharIndexFromLine(textBox.GetLineFromCharIndex(textBox.SelectionStart))

#3


0  

Off the top of my head, I think you want the SelectionStart property.

在我的头脑中,我想你想要SelectionStart属性。