Instead of displaying a messagebox in case of any invalidated chars in your new label you can replace with valid chars for treeview or xml;
public static class StringExtension
{
public static string Clean(string s)
{
return new StringBuilder(s)
.Replace("&", "_and_")
.Replace("@", "-a-")
.Replace(" ", "_")
.Replace("!","-")
.Replace(".", "_")
.Replace(",", "")
.Replace("'", "")
.Replace("(","-")
.Replace (")", "-")
.ToString();
}
}
private void trwItems_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (e.Label != null)
{
if (e.Label.Length > 0)
{
if (e.Label.IndexOfAny(new char[] { '@', '.', ',', '!',' ', '|','<','>' }) == -1)
{
e.Node.EndEdit(false);
}
else
{
e.Node.Text = StringExtension.Clean(e.Label);
e.CancelEdit = true;
}
}
else
{
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
"Node Label Edit");
e.Node.BeginEdit();
}
}
}