如何让我的方法在winforms中等待按钮点击事件

时间:2015-11-26 13:20:59

标签: c# json winforms treeview

我有winForms项目,我的button_click 1添加了我的文档章节中的元素。我想在配置文件test.json中保存我的配置(它是由JSON格式编写的)。我通过选中复选框保存所选内容。后来我想加载选中的复选框。但是如何等待button_click1事件将被点击,我的复选框将会出现。我的加载文件如下所示:

 private void SaveConfig(string path)
        {
            var config = new DocConfig();
            config.Parts = new List<DocPart>();


            foreach (TreeNode node in treeView1.Nodes)
            {

                {
                    config.Parts.Add(new DocPart { NodeTitle = node.Text, NodeChecked = node.Checked });
                }


                    {
                        foreach (TreeNode child in node.Nodes)
                        {
                            config.Parts.Add(new DocPart { ChildTitle = child.Text, ChildChecked = child.Checked });
                        }
                    }


                var configString = config.SaveToString();
                File.WriteAllText(path, configString);

            }
        }
        private void LoadConfig(string path)
        {

            var cfgString = File.ReadAllText(path);
            var cfg = DocConfig.LoadFromString(cfgString);


            foreach (var part in cfg.Parts)
            {
                foreach (TreeNode node in treeView1.Nodes)
                {
                    if (part.NodeTitle == "chap1")
                    {
                        node.Checked = part.NodeChecked;
                    }
                    if (part.NodeTitle == "chap2")
                    {
                        node.Checked = part.NodeChecked;
                    }
                    if (part.NodeTitle == "chap3")
                    {
                        node.Checked = part.NodeChecked;
                    }
                }
            }
        }
    }
}

删除了button1_click()并添加了此类

private void Initializetreeview();

    treeView1.Nodes.Add(new TreeNode("chapter1") { Tag = @"\include {chapter1}" }); ;
                treeView1.Nodes.Add(new TreeNode("chapter2") { Tag = @"\include {chapter2}" });
                treeView1.Nodes.Add(new TreeNode("chapter3") { Tag = @"\include {chapter3}" });

    treeView1.Nodes[0].Nodes.Add(new TreeNode("section1") { Tag = @"\input {sec1}" });
                treeView1.Nodes[1].Nodes.Add(new TreeNode("section2") { Tag = @"\input {sec2}" });
                treeView1.Nodes[2].Nodes.Add(new TreeNode("section3") { Tag = @"\input {sec3}" });

从添加的列表中删除button2_Click:

private void button2_Click(object sender, EventArgs e)
        {
            RemoveChecked(treeView1.Nodes);
        }

        void RemoveChecked(TreeNodeCollection nodes)
        {
            foreach (TreeNode checkedNode in FindCheckedNodes(nodes))
            {
                nodes.Remove(checkedNode);
            }
        }


        private List<TreeNode> FindCheckedNodes(TreeNodeCollection nodes)
        {
            List<TreeNode> checkedNodes = new List<TreeNode>();
            foreach (TreeNode node in nodes)
            {
                if (node.Checked)
                {
                    checkedNodes.Add(node);
                }
                else
                {
                    // find checked childs        
                    checkedNodes.AddRange(FindCheckedNodes(node.Nodes));
                }
            }
            return checkedNodes;
        }

这是我的加载事件处理程序,我在其中调用我的LoadConfig:

private void Form1_Load(object sender, EventArgs e)
        {
            Initializetreeview();
            var cmdArgs = Environment.GetCommandLineArgs();

            if (cmdArgs.Length == 1)
            {
                //MessageBox.Show("None file loaded as parameter");
            }
            if (cmdArgs.Length == 2)
            {
                //MessageBox.Show("JSON file is not loaded as parameter");
                var dconfFilename = cmdArgs[1];
                LoadConfig(dconfFilename);
            }
        }

1 个答案:

答案 0 :(得分:0)

因为LoadConfig方法假定TreeView控件已经包含具有名称的节点,所以在执行LoadConfig方法之前需要初始化TreeView

//Create method which initialize treeview control with your nodes
private void InitializeTreeView()
{
    this.treeView1.Nodes.Add(new TreeNode("chapter1") { Tag = @"\include {chapter1}" });
    this.treeView1.Nodes.Add(new TreeNode("chapter2") { Tag = @"\include {chapter2}" });
    this.treeView1.Nodes.Add(new TreeNode("chapter3") { Tag = @"\include {chapter3}" });

    this.treeView1.Nodes[0].Nodes.Add(new TreeNode("section1") { Tag = @"\input {sec1}" });
    this.treeView1.Nodes[1].Nodes.Add(new TreeNode("section2") { Tag = @"\input {sec2}" });
    this.treeView1.Nodes[2].Nodes.Add(new TreeNode("section3") { Tag = @"\input {sec3}" });
}

private void Form_Load(Object sender, EventArgs e)
{
    this.InitializeTreeView();

    //Now you can load your config
    var cmdArgs = Environment.GetCommandLineArgs();

    if (cmdArgs.Length == 1)
    {
        //MessageBox.Show("None file loaded as parameter");
    }
    if (cmdArgs.Length == 2)
    {
        //MessageBox.Show("JSON file is not loaded as parameter");
        var dconfFilename = cmdArgs[1];
        LoadConfig(dconfFilename);
    }
}