Вы можете управлять положением курсора (и выбором) с помощью свойств TextBox.SelectionStart
и TextBox.SelectionLength
.
Пример, если вы хотите переместить курсор до 3-го набора символов SelectionStart = 2
и SelectionLength = 0
.
Итак, в качестве приложения Windows Forms - решение вашей проблемы
public class TextBoxEx : TextBox
{
public TextBoxEx()
{ }
public void GoTo(int line, int column)
{
if (line < 1 || column < 1 || this.Lines.Length < line)
return;
this.SelectionStart = this.GetFirstCharIndexFromLine(line - 1) + column - 1;
this.SelectionLength = 0;
}
public int CurrentColumn
{
get { return this.SelectionStart - this.GetFirstCharIndexOfCurrentLine() + 1; }
}
public int CurrentLine
{
get { return this.GetLineFromCharIndex(this.SelectionStart) + 1; }
}
}
ИЛИ
Просто добавьте этот класс в свой проект,
public static class Extentions
{
public static void GoTo ( this TextBox Key , int Line , int Character )
{
if ( Line < 1 || Character < 1 || Key . Lines . Length < Line )
return;
Key . SelectionStart = Key . GetFirstCharIndexFromLine ( Line - 1 ) + Character - 1;
Key . SelectionLength = 0;
Key . Focus ( );
}
}
После добавления этого класса в ваш проект вы можете легко перемещаться по TextBox с помощью
TextBox . GoTo ( 1 , 1 ); // Navigate to the 1st line and the 1st character :)
Надеюсь, эта помощь.