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

C# The TreeView.Nodes.find method returns a collection of length > 0, but the index of returned node is 0

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

树视图由这些值组织。意思是分区是一个 child 的比赛之夜,玩家名字是一个 child 的分区。

当我将一个播放器添加到 TreeView 中时,其分区已经存在,搜索该节点 returns 一个树节点的集合,但该节点的索引为 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; //THIS INDEX IS ALWAYS 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
            }
        }
    }