Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
This is my tree example...

D1
1____D4
1 1_____D9
1 1 1_____S001
1 1 1_____S002
1 1 1_____S003
1 1
1 1_____D10
1 1 1_____S004
1 1 1_____S005
1 1
1 1_____D11
1 1_____S006
D2
1____D5
1 1_____D12
1 1 1_____S007
1 1
1 1_____S008
1
1_____D6
1 1____S009
1 1____S010
D3
1_____D16
1
1_____S011

i get the out put like this...

D1//D4//D9//S001
D1//D4//D9//S002
D1//D4//D9//S003
D1//D4//D10//S004
D1//D4//D10//S005
D1//D4//D11//S006
D2//D5//D12//S007
D2//D5//S008
D2//D6//S009
D2//D6//S010
D3//D16
D3//S011

This is stored in an arraylist.
but required out put as follow.
its very urgent.
using string comparisons also OK for me.
but i need this out put.

D1//D4//D9//S001//S002//S003
D1//D4//D10//S004//S005
D1//D4//D11//S006
D2//D5//D12//S007
D2//D5//S008
D2//D6//S009//S010
D3//D16//S011



This is my code...

MSIL
private void button2_Click(object sender, EventArgs e)
       {
           ArrayList finalTVStrings = new ArrayList();
           TreeNodeCollection nodes = tvAllData.Nodes;
           for (int i = 0; i <= nodes.Count-1;i++)
           {
               // Select the root node
                   TreeNodeCollection Cnodes = tvAllData.Nodes[i].Nodes;
                   if (Cnodes.Count > 0)
                   {
                       //tvAllData.SelectedNode = Cnodes[0];
                       for (int j = 0; j <= Cnodes.Count - 1; j++)
                       {
                           TreeNodeCollection SCnodes = tvAllData.Nodes[i].Nodes[j].Nodes;
                           if (SCnodes.Count > 0)
                           {
                               for (int SC = 0; SC <= SCnodes.Count - 1; SC++)
                               {
                                   TreeNodeCollection Sensornodes = tvAllData.Nodes[i].Nodes[j].Nodes[SC].Nodes;
                                   if (Sensornodes.Count > 0)
                                   {
                                       for (int Sen = 0; Sen <= Sensornodes.Count - 1; Sen++)
                                       {
                                           tvAllData.SelectedNode = Sensornodes[Sen];
                                          //MessageBox.Show(tvAllData.SelectedNode.FullPath, "Final");
                                           //MessageBox.Show(Sensornodes[Sen].Text, "Sensors");
                                           finalTVStrings.Add(tvAllData.SelectedNode.FullPath);
                                       }
                                   }
                                   else
                                   {
                                       tvAllData.SelectedNode = SCnodes[SC];
                                       //MessageBox.Show(tvAllData.SelectedNode.FullPath);
                                       //MessageBox.Show(SCnodes[SC].Text, "SubChild");
                                       finalTVStrings.Add(tvAllData.SelectedNode.FullPath);
                                   }
                               }
                           }
                           else
                           {
                               tvAllData.SelectedNode = Cnodes[j];
                               //MessageBox.Show(tvAllData.SelectedNode.FullPath);
                               //MessageBox.Show(Cnodes[j].Text, "SChild");
                               finalTVStrings.Add(tvAllData.SelectedNode.FullPath);
                           }
                       }
                   }
                   else
                   {
                      tvAllData.SelectedNode = nodes[i];
                       //MessageBox.Show(tvAllData.SelectedNode.FullPath);
                      // MessageBox.Show(Cnodes[i].Text, "Child");
                       finalTVStrings.Add(tvAllData.SelectedNode.FullPath);
                   }
                   ArrayList a = showlist(finalTVStrings);

           }
       }
Posted
Updated 25-Feb-11 3:34am
v3
Comments
Tarun.K.S 25-Feb-11 7:48am    
You should have stored it in a Dictionary instead of an ArrayList. The picture would more clearer for us if you could show your code.
R. Giskard Reventlov 25-Feb-11 8:07am    
Isn't this a repost? Didn't you get an answer the other day: besides no one here responds well to the childish and rather desperate call of 'plz help' and/or 'urgent'.
ajitha.pusapati 25-Feb-11 9:33am    
sorry..
Tarun.K.S 25-Feb-11 8:56am    
A big code with so many for loops and if-else statements! It's making my head spin!
ajitha.pusapati 25-Feb-11 9:28am    
try to make solution...
plz think my situation. i have just 7 months of exp and i dont have any guidance.
i have to develop this.

1 solution

Why would you want it in that format:
D1//D4//D9//S001//S002//S003
How can you tell the difference between:
VB
D1
1____D4
1 1_____D9
1 1 1_____S001
1 1 1_____S002
1 1 1_____S003
and
VB
D1
1____D4
1 1_____D9
1 1 1_____S001
1 1 1 1_____S002
1 1 1 1_____S003


(Sorry to make it an answer, but you can't put pictures well in comments).


"thanks for ur reply.
in my in button click event i have to sent the above output.
in my tree view D9 have 3 childes. that childes does not have any childes.
i want last node Path which is having childes and i have to concatenate that node childes which is doesn't have any childes.

I have to send to an embedded device. they required like that bcz they have less memory. plz help me"


That's a little complex (and so is your solution, but Hey! It works!
As a result, I'm almost afraid to touch your version, and would add an extra coding step as a post-process to compact your data.

Here is my version (I use Lists instead of ArrayList because they are type safe):

List<string> uncompacted = new List<string>();
uncompacted.Add("D1//D4//D9//S001");
uncompacted.Add("D1//D4//D9//S002");
uncompacted.Add("D1//D4//D9//S003");
uncompacted.Add("D1//D4//D10//S004");
uncompacted.Add("D1//D4//D10//S005");
uncompacted.Add("D1//D4//D11//S006");
uncompacted.Add("D2//D5//D12//S007");
uncompacted.Add("D2//D5//S008");
uncompacted.Add("D2//D6//S009");
uncompacted.Add("D2//D6//S010");
uncompacted.Add("D3//D16");
uncompacted.Add("D3//S011");

List<string> compacted = new List<string>();
string basis = "****";
StringBuilder sb = new StringBuilder();
foreach (string s in uncompacted)
    {
    if (s.StartsWith(basis))
        {
        // Same as previous string
        sb.Append(s.Substring(basis.Length));
        }
    else
        {
        // New basis
        if (sb.Length != 0)
            {
            compacted.Add(sb.ToString());
            }
        basis = s.Substring(0, s.LastIndexOf('/'));
        sb = new StringBuilder(s);
        }
    }
if (sb.Length != 0)
    {
    compacted.Add(sb.ToString());
    }

foreach (string s in compacted)
    {
    Console.WriteLine(s);
    }



"S001
S002
S003 are sensors. that sensors doesnot have any childes. so i need like that and also evry Di node must have one or more sendores."


Yes... and the output of my code is:
D1//D4//D9//S001/S002/S003
D1//D4//D10//S004/S005
D1//D4//D11//S006
D2//D5//D12//S007
D2//D5//S008
D2//D6//S009/S010
D3//D16/S011
Which is what you asked for...


"Thanks for ur replay but its not give derided out put."


Oh come on! That's not exactly difficult to fix, is it! Change one line, add just "- 1":
MIDL
basis = s.Substring(0, s.LastIndexOf('/') - 1);




"its working...
i have to inherit into my code. thanks allot.
May i know what is type safe.
Why u using list instead of array list."



When you use an ArrayList, you can add any items to it:
C#
ArrayList ar = new ArrayList();
ar.Add("hello");
ar.Add(new Point(1, 2));
ar.Add(new formMain());


That means when you access the items later, you have to check what type they are and cast them appropriately. If your code does not check for a particular type then it will either be unable to handle it, or will fail at run time.

If you use a List<T>, then you can only store one type (or derivatives) in the list. As a result, you don't have to cast, or check, because the compiler does it for you. If you try to a add different type to the list, the compiler complains and your mistake is caught earlier.

C#
List<string> uncompacted = new List<string>();

Declares a list of strings, and any attempt to add an int or Point will be complained about by the compiler. We can then use the content much more readably:
C#
foreach (string s in uncompacted)
   {
   ...
   }


That's what we mean by "type safe": you can't add the wrong type to a type safe list.
 
Share this answer
 
v5
Comments
OriginalGriff 25-Feb-11 9:41am    
Answer updated
ajitha.pusapati 25-Feb-11 9:49am    
S001
S002
S003 are sensors. that sensors doesnot have any childes. so i need like that and also evry Di node must have one or more sendores.
OriginalGriff 25-Feb-11 9:56am    
Answer updated - again
ajitha.pusapati 25-Feb-11 10:04am    
Thanks for ur replay but its not give derided out put.
OriginalGriff 25-Feb-11 10:13am    
Fixed!

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