Treeview issue while renaming the node
Hi all i have written a code to rename the node of a treeview. While editing if i erase all the text and hit enter it is getting renamed but if the user enter some text with an extensioin .txt then only i would like to rename that name.
I debugged my solution erasing all the text gives "" so that it is not checking the condition what to do to raise an error message if it is left as that i speci开发者_开发知识库fy
This is my code
//Contextmenu
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
string strOld = treeViewACH.SelectedNode.ToString();
treeViewACH.SelectedNode.BeginEdit();
}
//To show the context menu on the selected node
private void treeViewACH_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (treeViewACH.SelectedNode.Parent != null)
{
string strSwitch = treeViewACH.SelectedNode.Parent.Text;
switch (strSwitch)
{
case "FileHeader":
//string strOld = treeViewACH.SelectedNode.Text.ToString();
contextMenuStrip1.Show(treeViewACH, e.Location);
break;
}
}
else
{
// MessageBox.Show("Left clicked");
}
}
}
// To rename
private void treeViewACH_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (e.Label.IndexOfAny(new char[]{'\\',
'/', ':', '*', '?', '"', '<', '>', '|'}) != -1)
{
MessageBox.Show("Invalid tree node label.\n" +
"The tree node label must not contain " +
"following characters:\n \\ / : * ? \" < > |",
"Label Edit Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
e.CancelEdit = true;
return;
}
}
From what I can tell of your question, you are trying to make it so that a user can only change the text of the tree node to a string value that ends with ".txt". Assuming that is what you're trying to do, your AfterLabelEdit logic could be changed to something like:
private void treeViewACH_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (!e.Label.EndsWith(".txt"))
{
MessageBox.Show("Invalid tree node label.\n" +
"The tree node label must end with " +
"the extension: .txt",
"Label Edit Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
e.CancelEdit = true;
return;
}
}
You should go through details to get rid off this problem http://www.codeproject.com/KB/tree/CustomizedLabelEdit.aspx
精彩评论