1

私は Treeview と AfterLabelEdit 関数で遊んでおり、IM は検証後、編集に戻る前に MessageBox を 2 回表示するという問題を抱えています。ここで私が間違っているかもしれないことを誰でも見てください。

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

    }
4

0 に答える 0