问题TreeNode.BeginEdit() [英] Problem with TreeNode.BeginEdit()

查看:370
本文介绍了问题TreeNode.BeginEdit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的WinForms TreeView控件 AfterLabelEdit 事件的反应。下面的代码的片段:

I'm using WinForms TreeView and reaction to AfterLabelEdit event. Here's the snippet of the code:

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



的问题是,当用户不希望自动性质恶劣的修复,结不留在编辑模式。任何方式来解决这个问题。

The problem is that when user doesn't want automatic fix of bad character, node doesn't stay in edit mode. Any way to fix this?

推荐答案

有几件事情要记住:


  1. AfterLabelEdit 事件总是结束编辑模式,复活之后,即使你叫 BeginEdit 在事件处理的中间。您可以使用 TreeView.BeginInvoke 由具有编辑模式再次启动TreeView的就执行后越级这一点。的(注意:这不会创建一个新的线程或竞争条件,它只是延迟1窗口消息的方法)的上有一些问题,与此事件的这里(虽然它暗示什么,我认为这是一个糟糕的解决方案)

  2. e.Label 如果用户没有进行任何更改,所以当我们以蛙跳带BeginInvoke的,这是因为如果用户没有进行任何更改,所以我们还需要处理这种情况。

  3. BeginInvoke的是在这种情况下一个可接受的解决方法,你会发现它是在这种情况下非常可靠

  1. The AfterLabelEdit event always ends edit mode after it is raised, even if you call BeginEdit in the middle of your event handler. You can use TreeView.BeginInvoke to "leapfrog" this by having EditMode start up again after the TreeView does its thing. (NOTE: this does not create a new thread or race condition, it simply delays the method for 1 window message.) There is more information on some of the issues with this event here (though it suggests what I think is a worse solution).
  2. e.Label is null if the user didn't make any changes, so when we "leapfrog" with BeginInvoke, it is as if the user didn't make any changes, so we also need to handle that case.
  3. BeginInvoke is an acceptable workaround in this case, you should find it to be very reliable in this situation.

这工作对我非常好,使用.NET 2.0测试:

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;
    }

这篇关于问题TreeNode.BeginEdit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆