treeview AfterLabelEdit MessageBox显示两次

时间:2013-05-24 15:39:18

标签: c# treeview messagebox

我正在使用Treeview和AfterLabelEdit函数和IM有一个问题,在验证之后它会在返回编辑之前显示MessageBox两次。任何人都可以看到我在这里做错了什么。

    private void treeView1_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
    {
        var HostsXML = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Hosts.xml");

        XmlDocument doc = new XmlDocument();
        doc.Load(HostsXML);

        foreach (TreeNode pChild in e.Node.Parent.Nodes)
        {
            if (pChild.Text == e.Label)
            {
                // same name found, cancel the edit operation
                MessageBox.Show("That Name Cannot be Used.  Please Select a Different Name");
                e.CancelEdit = true;
                e.Node.BeginEdit();
                //treeView1.Nodes.Remove(treeView1.SelectedNode);
                return;
            }
        }

        if (e.Label != null)
        {
            if (e.Label.Length > 0)
            {
                if (String.IsNullOrEmpty(selectedNode))
                {
                    XmlNode rootNode = doc.SelectSingleNode("Servers");
                    XmlNode recordNode = rootNode.AppendChild(doc.CreateNode(XmlNodeType.Element, "Server", ""));
                    recordNode.AppendChild(doc.CreateNode(XmlNodeType.Element, "Name", "")).InnerText = e.Label;
                }
                else
                {
                    XmlElement root = doc.DocumentElement;
                    XmlNodeList xnList = doc.SelectNodes("/Servers/Server[Name ='" + selectedNode + "']");

                    foreach (XmlNode xn in xnList)
                    {
                        xn["Name"].InnerText = e.Label;
                    }
                }
            }
            else
            {
                MessageBox.Show("You Did Not Enter a Valid Name:1");
                e.CancelEdit = true;
                e.Node.BeginEdit();
                //treeView1.Nodes.Remove(treeView1.SelectedNode);
                return;
            }
        }
        else
        {
            e.CancelEdit = true;
            MessageBox.Show("You Did Not Enter a Valid Name: 2");
            e.Node.BeginEdit();
            //treeView1.Nodes.Remove(treeView1.SelectedNode);
            return;
        }

selectedNode = null;
    doc.Save(HostsXML);

    }

0 个答案:

没有答案