Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to restrict somebody from entering text in a tree node. I want to check after "treeView1_AfterLabelEdit" that if the text inserted is already saved. If yes, then the inserted text should be changed to "New". How to do so?
Posted
Updated 25-Jan-23 11:50am
v4
Comments
ZurdoDev 20-Feb-14 8:25am    
There are plenty of people who can help but you'll need to be clear on what you need help with. Where are you stuck?

Click on Improve Question, add relevant code, and show us where you are stuck so that we can help.
agent_kruger 20-Feb-14 8:43am    
sir, is it alright now?
ZurdoDev 20-Feb-14 8:52am    
Better. But it still does not show where you are stuck. Does Solution 1 help?
agent_kruger 20-Feb-14 9:45am    
now see the question sir, this is the best explanation i could give. Please help me if you could understand it.
ZurdoDev 20-Feb-14 9:52am    
It doesn't show where you are stuck. If I understand you right, you need to check your db to see if the value exsits. If so, don't save to db and change to new. There is a lot of code to do that so which point are you stuck on?

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)
						{
						// Stop editing without canceling the label change.
						e.Node.EndEdit(false);
						}
					else
						{
						/* Cancel the label edit action, inform the user, and
						   place the node in edit mode again. */
						e.Node.Text = StringExtension.Clean(e.Label);
						e.CancelEdit = true;
						//MessageBox.Show("Invalid tree node label.\n" +
						//   "The invalid characters are: '@','.', ',', '!', empty space",
						//   "Node Label Edit");
						//e.Node.BeginEdit();
						}
					}
				else
					{
					/* Cancel the label edit action, inform the user, and
					   place the node in edit mode again. */
					e.CancelEdit = true;
					MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
					   "Node Label Edit");
					e.Node.BeginEdit();
					}
				}

			
			}
 
Share this answer
 
v4
Comments
Ralf Meier 26-Jan-23 5:44am    
Perhaps you can explain why you think that your "Solution" is an acceptable answer to this nearly 9 years old question ?
Also, the node must be updated after Label change as:

// Stop editing without canceling the label change.
e.Node.EndEdit(false);

//now, update node by Text (this also changes node.fullpath)
e.Node.Text = e.Label;


See MS site, too, below.

TreeNode.EndEdit(Boolean) Method (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Comments
Richard Deeming 12-Jun-18 11:10am    
Asked, answered, and solved FOUR YEARS AGO.
C#
/* Get the tree node under the mouse pointer and 
   save it in the mySelectedNode variable. */ 
private void treeView1_MouseDown(object sender, 
  System.Windows.Forms.MouseEventArgs e)
{
   mySelectedNode = treeView1.GetNodeAt(e.X, e.Y);
}

private void menuItem1_Click(object sender, System.EventArgs e)
{
   if (mySelectedNode != null && mySelectedNode.Parent != null)
   {
      treeView1.SelectedNode = mySelectedNode;
      treeView1.LabelEdit = true;
      if(!mySelectedNode.IsEditing)
      {
         mySelectedNode.BeginEdit();
      }
   }
   else
   {
      MessageBox.Show("No tree node selected or selected node is a root node.\n" + 
         "Editing of root nodes is not allowed.", "Invalid selection");
   }
}

private void treeView1_AfterLabelEdit(object sender, 
         System.Windows.Forms.NodeLabelEditEventArgs e)
{
   if (e.Label != null)
   {
     if(e.Label.Length > 0)
     {
        if (e.Label.IndexOfAny(new char[]{'@', '.', ',', '!'}) == -1)
        {
           // Stop editing without canceling the label change.
           e.Node.EndEdit(false);
        }
        else
        {
           /* Cancel the label edit action, inform the user, and 
              place the node in edit mode again. */
           e.CancelEdit = true;
           MessageBox.Show("Invalid tree node label.\n" + 
              "The invalid characters are: '@','.', ',', '!'", 
              "Node Label Edit");
           e.Node.BeginEdit();
        }
     }
     else
     {
        /* Cancel the label edit action, inform the user, and 
           place the node in edit mode again. */
        e.CancelEdit = true;
        MessageBox.Show("Invalid tree node label.\nThe label cannot be blank", 
           "Node Label Edit");
        e.Node.BeginEdit();
     }
   }
}

TreeView.LabelEdit Property[^]

-KR
 
Share this answer
 
Comments
agent_kruger 20-Feb-14 9:49am    
"e.CancelEdit = true;" did the work, sir. Thank you
Krunal Rohit 20-Feb-14 10:02am    
Glad I could help :)
-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900