覚えておくべきいくつかのこと:
- イベントハンドラーの途中で
AfterLabelEdit
呼び出した場合でも、イベントは発生後に常に編集モードを終了します。TreeViewが処理を実行した後、EditModeを再起動することで、これを「飛躍」させる BeginEdit
ことができます。(注:これは新しいスレッドまたは競合状態を作成せず、1ウィンドウメッセージのメソッドを遅らせるだけです。) このイベントに関するいくつかの問題に関する詳細情報がここにあります(ただし、これはより悪い解決策であると私が考えるものを示唆しています) 。TreeView.BeginInvoke
e.Label
これはnull
、ユーザーが変更を加えなかった場合です。したがって、BeginInvokeで「飛躍」すると、ユーザーが変更を加えなかったかのようになり、その場合も処理する必要があります。
- この場合、BeginInvokeは許容できる回避策であり、この状況では非常に信頼性が高いことがわかります。
これは、.NET 2.0でテストした場合、非常にうまく機能します。
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
//we have to handle both the first and future edits
if ((e.Label != null && e.Label.Contains("|") || (e.Label == null && e.Node.Text.Contains("|"))))
{
if (WantAutofix())
{
e.CancelEdit = true;
if(e.Label != null)
e.Node.Text = e.Label.Replace('|', '_');
else
e.Node.Text = e.Node.Text.Replace('|', '_');
}
else
{
//lets the treeview finish up its OnAfterLabelEdit method
treeView1.BeginInvoke(new MethodInvoker(delegate() { e.Node.BeginEdit(); }));
}
}
}
private bool WantAutofix()
{
return MessageBox.Show("You entered a |, you want me to AutoFix?", String.Empty, MessageBoxButtons.YesNo) == DialogResult.Yes;
}