添加treeview节点的问题 [英] problem with adding treeview node

查看:49
本文介绍了添加treeview节点的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (!treeView1.Nodes[i].Nodes[j].Text.Contains(mode01)) { treeView1.Nodes[i].Nodes.Add(mode01); }


如果[treeView1.Nodes [i] .Nodes [j] .Text]不包含mode01("mp"),则将其添加,仅添加一次.并非每次都找到它!到目前为止,一切正常.
我想为[treeView1.Nodes [i] .Nodes [j] .Nodes.Add(mode02);]使用相同的逻辑,但我无法实现.当前,他每次找到mode02("usa")都会添加.

我插入了[for(int k = 0; k< treeView1.Nodes [i] .Nodes [j] .Nodes.Count; k ++)]],但是我遇到了一个非常新的奇怪错误,关于无限循环是从资源中吃了太多.
我尽力解释.欢迎任何问题.
包含数据的文件具有字符串[Human"mp/usa/rifle" 0xb6b3]的变体.


if [treeView1.Nodes[i].Nodes[j].Text] does not contain mode01("mp"), add it, but one time only. Not every time you find it!So far, is working fine.
Same logic I want for [treeView1.Nodes[i].Nodes[j].Nodes.Add(mode02);] but I can not achieve it. Currently he is adding every time it find mode02("usa").

I have inserted a [for (int k = 0; k < treeView1.Nodes[i].Nodes[j].Nodes.Count; k++)] but I catch a very new and strange error, about an infinite loop that is eating too much from resources.
I tried to explain as best I could. Any questions are welcome.
The file that contain the data, have variations of string[Human "mp/usa/rifle" 0xb6b3].

foreach (var item in LListofEntity)//for    Human "mp/usa/rifle" 0xb6b3
{
    if (item.Contains("Human"))
    {
        for (int i = 0; i < treeView1.Nodes.Count; i++)
        {
            if (treeView1.Nodes[i].Text.Contains("Human"))
            {
                //extract "-string-"
                string cod0 = @""".*""";
                string itemCleanned = Regex.Match(item.ToString(), cod0).Value;
                //add mp
                string cod1 = @"""\w+";
                string mode01 = Regex.Match(itemCleanned, cod1).Value.Replace("\"", "");
                //add usa
                string cod2 = @"/.*/";
                string mode02 = Regex.Match(itemCleanned, cod2).Value.Replace("/", "");
                if (itemCleanned.Contains(mode01))   
                {
                tryagain:
                    if (treeView1.Nodes[i].Nodes.Count == 0) 
                    { treeView1.Nodes[i].Nodes.Add(mode01); goto tryagain; }
                    else
                    {
                        for (int j = 0; j < treeView1.Nodes[i].Nodes.Count; j++)
                        {
                            if (!treeView1.Nodes[i].Nodes[j].Text.Contains(mode01)) 
                            { treeView1.Nodes[i].Nodes.Add(mode01); }
                            if (!treeView1.Nodes.ContainsKey(mode02))
                            {
                                treeView1.Nodes[i].Nodes[j].Nodes.Add(mode02);
                            } 

                        }
                    }
                }

            }

        }
        treeView1.ExpandAll();
    }
}

推荐答案

让我向您报告我乍看之下可能遇到的第一个问题:

Let me report to you the first problem I could catch at first glance:

for (int k = 0; k < treeView1.Nodes[i].Nodes[j].Nodes.Count; k++) {
    //insert some nodes somewhere
}



这里的问题是:根据插入方式的不同,Count的值可以以比k更快的速率增长,因此可能永远无法满足循环结束的条件.它可以是无限循环.换句话说,如果您需要精确地重复某些循环主体treeView1.Nodes[i].Nodes[j].Nodes.Count​​次,则需要一个非常不同的代码,如下所示:



The problem here is: depending on how you insert, the value of Count can grow at the same rate of faster than k, so the condition of the ending of the loop may never be met. It can be an infinite loop. In other words, if you need to repeat some loop body exactly treeView1.Nodes[i].Nodes[j].Nodes.Count times, you would need a very different code like this:

int nodeCount = treeView1.Nodes[i].Nodes[j].Nodes.Count;
for (int k = 0; k < nodeCount; ++k) { //this is weird, but ++k is faster than k++
    //insert some nodes somewhere
}



你看到区别了吗?

旁注:永远不要命名任何成员或变量,例如treeView1.它看起来像一个自动生成的代码,违反了(良好的)Microsoft命名约定.您应该始终将这些名称重命名为某种语义.当然,绝对不要使用ijk之类的名称.从理论上讲,这并不重要,但实际上,这是许多不便甚至是错误的常见来源.名称应具有语义含义,并且应更长:indexparentIndexchildIndexsourceIndextargetIndex等.

—SA



Do you see the difference?

A side note: never ever name any members or variables like treeView1. It looks like an auto-generated code which violates (good) Microsoft naming conventions. You should always rename those names to something semantic. And of course, never use the names like i, j or k. In theory, it does not matter, but in practice — a usual source of many inconveniences or even bugs. The names should have semantic meaning and be longer: index, parentIndex, childIndex, sourceIndex, targetIndex, etc.

—SA


经过大量痛苦的调试之后,我得出以下结论:

after a lot of painful debugging, I reach at this conclusion:

if (!treeView1.Nodes.ContainsKey(mode02))
                            {
                                treeView1.Nodes[i].Nodes[j].Nodes.Add(mode02);
                            }


错了!
正确的方法最好在下一个完整代码中查看:


That was wrong!
The correct way is best viewed in the next full code:

void Calculate()
 {
     LListofEntity.Clear();
     string cod = @"^((\t{).*\n)(\t\t.*\n)*(\t})*";//-blocks-
     foreach (var item in Regex.Matches(tot, cod, RegexOptions.Multiline)) LListofEntity.Add(item.ToString());/*<<<<<<<<<<<<<<<*/
     //-------------------------------------------------
     string cod114 = @"^\t{[A-Z]*[a-z]*"; //add [{Name ] to treeView1
     foreach (var item in LListofEntity)
     {
         string regd = Regex.Match(item, cod114).Value.TrimStart('\t');
         if (!treeView1.Nodes.ContainsKey(regd)) treeView1.Nodes.Add(regd, regd);
     }
     foreach (var item in LListofEntity)//for    Human "mp/usa/rifle" 0xb6b3
     {
         Application.DoEvents();
         if (item.Contains("Human"))
         {
             string name = Regex.Match(item.ToString(), @"^\t{Hum.*\n").Value;
             //extract "-string-"
             string itemCleanned = Regex.Match(item.ToString(), @""".*""").Value;            //string cod0 = @""".*""";
             //add mp
             string mode01 = Regex.Match(itemCleanned, @"""\w+").Value.Replace("\"", "");    //string cod1 = ;//"\w+   \w*/  "\w+/
             //add usa
             string mode02 = Regex.Match(itemCleanned, @"/.*/").Value.Replace("/", "");      //string cod2 = @"/.*/";// {/.*/ for [/]}    {"[a-z]*([0-9]*)_ for [_]}
             //add weapon
             string weapon = Regex.Match(itemCleanned, @"/\w*""").Value.Replace("/", "").Replace("\\", "").Replace("\"", ""); // /.*/
             string mode03 = Regex.Match(weapon, @"[A-Z]*[a-z]*").Value;
             //add weaponVersion???? -how to make the logic here????
             string mode04 = weapon.Replace(mode03, "");


             //add index -the 0xa666 part
             string mode05 = Regex.Match(name, @"0x[\w*\d*]").Value;


             for (int i = 0; i < treeView1.Nodes.Count; i++)
             {
                 if (treeView1.Nodes[i].ToString().Contains("Human"))
                 {
                     //--------------------------------------------------------
                     // +1 mp
                     if (!treeView1.Nodes[i].Nodes.ContainsKey(mode01))
                     {
                         treeView1.Nodes[i].Nodes.Add(mode01, mode01);
                     }
                     //nextHuman:
                     for (int j = 0; j < treeView1.Nodes[i].Nodes.Count; j++)
                     {
                         // +1 usa
                         if (!treeView1.Nodes[i].Nodes[j].Nodes.ContainsKey(mode02))
                         {
                             treeView1.Nodes[i].Nodes[j].Nodes.Add(mode02, mode02);
                         }
                         //nextweapon
                         for (int k = 0; k < treeView1.Nodes[i].Nodes[j].Nodes.Count; k++)
                         {
                             // +1 weapon
                             if (!treeView1.Nodes[i].Nodes[j].Nodes[k].Nodes.ContainsKey(weapon))
                             {
                                 //pick the right human
                                 if (treeView1.Nodes[i].Nodes[j].Nodes[k].ToString().Contains(mode02))//who[j] = treeView1.Nodes[i].Nodes[j].Nodes[k].Text + "-" + mode03; //name.ToString();
                                 {
                                     treeView1.Nodes[i].Nodes[j].Nodes[k].Nodes.Add(weapon, weapon);
                                 }
                             }
                             //weaponVersion?
                             //{Human "mp/usa/rifle_3_" 0xa005
                             //here I got into deeper problems, but I am pleased so far.!.
                         }
                     }



                 }
             }
         }
     }
     treeView1.ExpandAll();
 }


该应用程序的++部分都可以正常工作.但是我会记住它,以备将来遇到.没错,它的执行速度更快.经过测试.
谢谢你的贡献.


The ++ part is working good either ways for this app; But I will remember it for future encounters. You are right, it perform faster.Tested.
Thanks for the imput.


这篇关于添加treeview节点的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆