Forums: 公開討議 (Thread #44492)

Caretを、指定した行/列インデックスへ移動する方法を教えて下さい。 (2021-08-07 16:31 by kazmax #87863)

①Enterで改行後、改行前の行の先頭に全角空白があれば数を数えて、同じ数の全角空白を新らしい行の先頭に自動挿入する。
②挿入後、Caretを、挿入された全角空白の最後の位置に移動する。
という機能を作っています。

①は出来るのですが、②が出来ません。
どのようにすればよいでしょうか。

実装は下記のとおりです。

private void AzukiEditor_TextChanged(object sender, EventArgs e)
{
if (LastInputedKey == Keys.Enter)
{
int newLineIndex = AzukiEditor.GetLineIndexFromCharIndex(AzukiEditor.CaretIndex);
if (newLineIndex != 0)
{
string prevLine = AzukiEditor.Document.GetLineContent(newLineIndex - 1);
if (prevLine.StartsWith(" "))
{
string headSpaces = "";
for (int i = 0; i < prevLine.Length; i++)
{
if (prevLine[i] == ' ')
{
headSpaces += " ";
}
else
{
break;
}
}
AzukiEditor.TextChanged -= AzukiEditor_TextChanged;
AzukiEditor.Document.Text = AzukiEditor.Document.Text.Insert(AzukiEditor.CaretIndex, headSpaces);
AzukiEditor.TextChanged += AzukiEditor_TextChanged;
headSpaces = "";
}
}
}
}

Reply to #87863×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

Re: Caretを、指定した行/列インデックスへ移動する方法を教えて下さい。 (2021-08-07 22:45 by kazmax #87864)

自己解決しました。

private void AzukiEditor_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
int prevLineIndex = AzukiEditor.GetLineIndexFromCharIndex(AzukiEditor.Document.CaretIndex);
string prevLineText = AzukiEditor.Document.GetLineContent(prevLineIndex);

string headSpaces = "";
if (prevLineText.StartsWith(" "))
{
for (int i = 0; i < prevLineText.Length; i++)
{
if (prevLineText[i] == ' ')
{
headSpaces += " ";
}
else
{
break;
}
}
e.Handled = true;
AzukiEditor.Document.Replace(Environment.NewLine + headSpaces, AzukiEditor.Document.CaretIndex, AzukiEditor.CaretIndex);
}
}
}
Reply to #87863

Reply to #87864×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login