Problem with TreeNode.BeginEdit ()

I use WinForms TreeView and respond to the AfterLabelEdit event. Here is the code snippet:

 if (e.Label.Contains("|")) { if (WantAutofix()) { label = e.Label.Replace('|', '_'); } else { e.CancelEdit = true; e.Node.BeginEdit(); return; } } 

The problem is that when the user does not want to automatically correct the bad character, the node does not remain in edit mode. Any way to fix this?

+6
source share
4 answers

A few things to keep in mind:

  • The AfterLabelEdit event always ends the editing mode after raising it, even if you call BeginEdit in the middle of the event handler. You can use TreeView.BeginInvoke to "skip" this by running EditMode after the TreeView does its job. (NOTE: this does not create a new thread or race condition, it just delays the method for 1 message box.) There is more information about some issues with this event here (although this suggests what I consider to be the worst solution).
  • e.Label null if the user has not made any changes, so when we “skip” with BeginInvoke, it is as if the user has not made any changes, so we also need to handle this case.
  • BeginInvoke is an acceptable workaround in this case, you should find it very reliable in this situation.

This works very well for me, tested with .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; } 
+2
source

You can try to make BeginEdit () asynchronously:

 private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { if (e.Label.Contains("|")) { if (WantAutofix()) { } else { e.CancelEdit = true; BeginInvoke(new ActionDelegate(new NodeBeginEditAsync(e.Node).Execute)); return; } } } public class NodeBeginEditAsync { private readonly TreeNode _node; public NodeBeginEditAsync(TreeNode node) { _node = node; } public void Execute() { _node.BeginEdit(); } } public delegate void ActionDelegate(); 

Thus, CancelEdit has a chance to shut down before the new BeginEdit tries to capture.

0
source

Use EndEdit and replace the "bad character" if the user needs an automatic fix.

0
source
 try this... TreeNode node = tv.SelectedNode; if (tv.SelectedNode.Parent == null) { node.TreeView.LabelEdit = false; } else { node.Text = FieldName.Text; if (node == null) { return; } node.TreeView.LabelEdit = true; node.BeginEdit(); } 
0
source

Source: https://habr.com/ru/post/892139/


All Articles