У меня есть TextBox
txtEditor
. Я хочу найти ближайшие разрывы строк и выбрать его
Пример 1: с "без выбора"
предположим, что выделение / курсор *
this is the 1st line of text
this is *the 2nd line of text
this is the 3rd line of text
Я хочу расширить выделение так, чтобы выделение теперь было
this is the 1st line of text
*this is the 2nd line of text*
this is the 3rd line of text
Пример 2: с выбором
this is the 1st line of text
this is *the 2nd line* of text
this is the 3rd line of text
Я хочу расширить выбор так, чтобы выбор был теперь
this is the 1st line of text
*this is the 2nd line of text*
this is the 3rd line of text
Обновление: возможное решение
Я нашел возможное решение, интересно, кто-нибудь нашел лучшее решение?
string tmp = txtEditor.Text;
int selStart = txtEditor.SelectionStart;
int selLength = txtEditor.SelectionLength;
int newSelStart;
int newSelLength;
string tmp1 = tmp.Substring(0, selStart);
if (tmp1.LastIndexOf(Environment.NewLine) < 0)
{
newSelStart = 0;
}
else
{
newSelStart = tmp1.LastIndexOf(Environment.NewLine) + Environment.NewLine.Length;
}
tmp1 = tmp.Substring(selStart);
if (tmp1.IndexOf(Environment.NewLine) < 0)
{
newSelLength = tmp.Length;
}
else
{
newSelLength = tmp1.IndexOf(Environment.NewLine) + selStart - newSelStart;
}
txtEditor.SelectionStart = newSelStart;
txtEditor.SelectionLength = newSelLength;