【问题标题】:First item of a treeview is null树视图的第一项为空
【发布时间】:2017-01-02 10:48:59
【问题描述】:

我有一个线程将填充 TreeNode 集合以不阻塞主线程(Windows 窗体)。所以我通过这些行调用 TreeView.Nodes.Add 命令:

private void generateListeItems()
        {
            try
            {
                Item_Coll resultSet = connector.command_LIST();
                if(mainFormInvoke != null)
                    mainFormInvoke(formProgressBar, -1);
                TreeNode racine = new TreeNode();
                TreeNode NET = racine.Nodes.Add("Net");
                TreeNode LON = NET.Nodes.Add("LON");
                List<string> devices = new List<string>();

                TreeNode lastDevice = null;
                int lastDevice_index = -1;
                TreeNode lastDevice_fb = null;
                int lastDevice_fb_index = -1;
                int percentage = 0, lastPercentage = 0, treatedItem = 0;
                int nombreIndex = resultSet.Item.Count<Item>();
                foreach (Item index in resultSet.Item)
                {
                    string[] substring = index.UCPTname.Split('/');
                    if (lastDevice == null || substring[2] != lastDevice.Text)
                    {
                        lastDevice = LON.Nodes.Add(substring[2]);
                        lastDevice_index++;
                        lastDevice_fb_index = -1;
                    }

                    if (lastDevice_fb == null || substring[3] != lastDevice_fb.Text)
                    {
                        lastDevice_fb = lastDevice.Nodes.Add(substring[3]);
                        lastDevice_fb_index++;
                    }

                    lastDevice_fb.Nodes.Add(substring[4]);
                    dpDictionnary[index.UCPTname] = index;
                    lastPercentage = percentage;
                    percentage = ++treatedItem * (nombreIndex / 100);
                    if(percentage != lastPercentage)
                        mainFormInvoke(formProgressBar, 1);
                }

                mainFormInvoke((updatingListeInUI)liste.Nodes.Add, racine);
                mainFormInvoke(consoleWriter,string.Format("Connexion réussie, récupération de {0} éléments", resultSet.Item.Count<Item>()));
            }catch(SmartServerException e)
            {
                mainFormInvoke(consoleWriter, e.ToString());
            }

        }

我得到了这个:

我不想要第一个 null 元素。我怎么能通过这个?

【问题讨论】:

  • TreeNode racine = new TreeNode(); TreeNode NET = racine.Nodes.Add("Net"); 你不想让它为空,但你让它为空?对我没有任何意义
  • 如果您不想要它,请丢弃 racine 节点。仅使用NET 节点。
  • 哦。是的,这很愚蠢。谢谢

标签: c# .net winforms treeview


【解决方案1】:
TreeNode racine = new TreeNode("Root");
TreeNode NET = racine.Nodes.Add("Net");
TreeNode LON = NET.Nodes.Add("LON");

如上图所示将默认节点设置为root

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    相关资源
    最近更新 更多