用于节点数组

时间:2015-11-05 15:21:23

标签: c# winforms treeview

我创建了一个自定义窗体,我已经发了一个TreeView。我在TreeView中有特定的父/子节点。虽然,对于某些节点,我想根据我开发的应用程序编程接口的输出数组创建不同的子节点。例如,如果我的输出是长度= 2的数组,我想在第一个子节点中创建2个子节点。到目前为止,我已经成功地完成了这项任务。但是,当我尝试连接我的子节点时,左键单击不同的方法,我的程序崩溃。

我附上了部分代码并附上了一些解释:

   public partial class FeatureTree : Form
{


    public TreeNode rootRootChildren01, rootRootChildren02;
    public object[] rootRootChildren;
    public TreeNode rootNode;
    public TreeNode rootChildNode01, rootChildNode02;


    public FeatureTree(AddinClass addin)
    {
        userAddin = addin;
        App = (api)userAddin.Application;
        InitializeComponent();
        PopulateTreeView();
        this.treeView1.MouseDown += new MouseEventHandler(treeView1_MouseDown);
        this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
    }

    public void PopulateTreeView()
    {
        // Clear the TreeView each time the method is called.
        treeView1.Nodes.Clear();

        rootNode = new TreeNode("Parent Node 1");
        rootChildNode01 = new TreeNode("Child1");
        rootChildNode02 = new TreeNode("Child2");
        // Parent root
        treeView1.Nodes.Add(rootNode);
        // firts children root 
        rootNode.Nodes.Add(rootChildNode01);

        // here I check whether the output array is nothing
        if (userAddin.outputArray != null)
        {
            // if the output is not null create userAddin.outputArray.Length children roots
            rootRootChildren = new object[userAddin.outputArray.Length];
            for (int i = 0; i < userAddin.outputArray.Length; i++)
            {

                rootRootChildren[i] = new TreeNode(userAddin.beasyShell[i].Name);
                rootChildNode01.Nodes.Add((TreeNode)rootRootChildren[i]);

            }
            rootRootChildren01 = (TreeNode)rootRootChildren[0];
            rootRootChildren02 = (TreeNode)rootRootChildren[1];
        }

        // second children root
        rootNode.Nodes.Add(rootChildNode02);

        treeView1.ShowPlusMinus = false;
        treeView1.ExpandAll();
        treeView1.ShowNodeToolTips = true;
    }

    void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Node == rootRootChildren01 && e.Button == MouseButtons.Left)
        {

            userAddin.method_1;

        }
        if (e.Node == rootRootChildren02 && e.Button == MouseButtons.Left)
        {

            userAddin.method_2;

        }

    }

以上代码效果很好。但是,如何根据rootRootChildren数组在treeView1_NodeMouseClick方法中设置右键单击事件?在这种情况下,只创建了两个节点。但是如果输出数组要大得多呢?

非常感谢提前!

0 个答案:

没有答案