C#TreeView.Nodes.find方法返回长度为>的集合。 0,但返回节点的索引为0

时间:2015-05-22 19:13:36

标签: c# treeview

我的表单上有一个TreeView,它由用户输入填充。有三个值:游戏之夜,分区和玩家名称。

Treeview由这些值组织。意义分裂是游戏之夜的孩子,玩家命名为分裂的孩子。

当我使用已存在的分区向TreeView添加播放器时,搜索该节点将返回一个treenode的集合,但该节点的索引为0 - 在尝试将播放器添加到该节点时导致错误分裂。

这是我的代码:

    private void AddPlayerToTreeView(string playerName, string division, DateTime selDate)
    {
        TreeNode tn = new TreeNode();
        string shrtDate = selDate.ToShortDateString();

        //check to see if the date exists (shortdate format)
        // if doesn't exist: create the Night node and call the function recursively
        // if exists, check to see if division exists:
        //   if doesn't exist: create the Division node and call the function recursively
        //   if exists, add player


        TreeNode[] tns = this.tview_roster.Nodes.Find(shrtDate, false); //find the date in the root nodes

        if(tns.Length == 0) //the date doesn't exist in the list
        {
            tn = this.tview_roster.Nodes.Add(shrtDate, shrtDate);
            tn.ImageIndex = 2; //date icon

            this.AddPlayerToTreeView(playerName, division, selDate);
        }
        else //date exists, try to find division within it
        {
            var parentNight = tns[0].Index; //save the index of the night node

            tns = this.tview_roster.Nodes.Find(division, true); //search child nodes

            if (tns.Length == 0) //division doesn't exist, create it (and child nodes in recursive call)
            {
                tn = this.tview_roster.Nodes[parentNight].Nodes.Add(division, division);
                tn.ImageIndex = 1; //division icon

                this.AddPlayerToTreeView(playerName, division, selDate);
            }
            else //division exists, add player
            {
                var parentDiv = tns[0].Index; //THIS INDEX IS ALWAYS 0

                tn = this.tview_roster.Nodes[parentNight].Nodes[parentDiv].Nodes.Add(playerName, playerName);
                tn.ImageIndex = 0; //player icon
            }
        }
    }

正如您所看到的,问题出现在这一行:

  

var parentDiv = tns [0] .Index; //这个指数总是0

任何帮助将不胜感激,谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

好吧,我发现了这个问题,并认为我可以回答我的问题,以防这可能对其他人有所帮助。

问题是我在搜索子节点(除法)时没有指向父节点(parentNight)。

这是我的更新和功能代码:

    private void AddPlayerToTreeView(string playerName, string division, DateTime selDate)
    {
        TreeNode tn = new TreeNode();
        string shrtDate = selDate.ToShortDateString();

        //check to see if the date exists (shortdate format)
        // if doesn't exist: create the Night node and call the function recursively
        // if exists, check to see if division exists:
        //   if doesn't exist: create the Division node and call the function recursively
        //   if exists, add player


        TreeNode[] tns = this.tview_roster.Nodes.Find(shrtDate, false); //find the date in the root nodes

        if(tns.Length == 0) //the date doesn't exist in the list
        {
            tn = this.tview_roster.Nodes.Add(shrtDate, shrtDate);
            tn.ImageIndex = 2; //date icon

            this.AddPlayerToTreeView(playerName, division, selDate);
        }
        else //date exists, try to find division within it
        {
            var parentNight = tns[0].Index; //save the index of the night node

            tns = this.tview_roster.Nodes[parentNight].Nodes.Find(division, false); //search child nodes

            if (tns.Length == 0) //division doesn't exist, create it (and child nodes in recursive call)
            {
                tn = this.tview_roster.Nodes[parentNight].Nodes.Add(division, division);
                tn.ImageIndex = 1; //division icon

                this.AddPlayerToTreeView(playerName, division, selDate);
            }
            else //division exists, add player
            {
                var parentDiv = tns[0].Index; //save the index of the division node

                tn = this.tview_roster.Nodes[parentNight].Nodes[parentDiv].Nodes.Add(playerName, playerName);
                tn.ImageIndex = 0; //player icon
            }
        }
    }