Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if (!treeView1.Nodes[i].Nodes[j].Text.Contains(mode01)) { treeView1.Nodes[i].Nodes.Add(mode01); }

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].

C#
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();
    }
}
Posted
Updated 28-Feb-12 6:15am
v5
Comments
Sergey Alexandrovich Kryukov 28-Feb-12 16:59pm    
I cannot believe it. Here we go again.
You do not specify what is "TreeView". There are at least three different types named "TreeView". Which one are you working with? Full type name, please.

And please tag the application type and/or UI library: WPF? Forms? Silverlight? ASP.NET? What?
--SA
Sergey Alexandrovich Kryukov 28-Feb-12 17:06pm    
Questions? Sure.

What is the required behavior?

Why doing a search in the tree by a sub-string at all?
--SA

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

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


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:

C#
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
}


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
 
Share this answer
 
after a lot of painful debugging, I reach at this conclusion:

C#
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:
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900