none
Copy All Element of a Treeview in External Application in My Treeview

    Question

  • Hi

    I have to copy all elements of  treeview of external application in a treeview of my application

    i know the handle of external treeview and i write this code but i have only the root element 

     IntPtr handle = IntPtr.Zero;
                handle = (IntPtr)Convert.ToInt32(textBox1.Text);
                AutomationElement el = AutomationElement.FromHandle(handle);
                TreeWalker walker = TreeWalker.RawViewWalker;
                for (AutomationElement child = walker.GetFirstChild(el);
                    child != null;
                    child = walker.GetNextSibling(child))
                {
                    //string stringa = String.Format(" (item {0} is a \"{1}\" with name \"{2}\"", i++, child.Current.LocalizedControlType, child.Current.Name);
                        MessageBox.Show(child.Current.Name);
                   }
            }

    Wacky Teseo

    Monday, September 30, 2013 3:43 PM

Answers

  • Hi Wacky,

    Phill provided a right way to get it resolved-- recursive function-- LoadR. I think this is the key Phill would like to show you. Generally, others just provide us an idea, and we need to understand and apply the idea with our code but not just copy and past.

    To avoid the errors you encountered, try below steps:

    1. remove the line: MessageBox.Show( walker.Text );

    2. Change LoadR to private void LoadR( TreeWalker walker,  AutomationElement el)

    3. Change all calls to LoadR to meet the new format of LoadR function.



    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, October 03, 2013 7:20 AM

All replies

  • Your loop will only handle root-level nodes.

    You have to allow for each node to have child nodes itself:

    private void X() 
    { 
       IntPtr handle = (IntPtr)Convert.ToInt32( textBox1.Text );
       AutomationElement el = AutomationElement.FromHandle( handle );
       TreeWalker walker = TreeWalker.RawViewWalker;
       LoadR( walker ); 
    }
    
    private void LoadR( TreeWalker walker ) 
    {
       //Handle this node 
       MessageBox.Show( walker.Text );
    
       // Scan for child nodes 
       for (AutomationElement child = walker.GetFirstChild(el) 
          ; child != null 
          ; child = walker.GetNextSibling(child) 
          )
       {
          LoadR( child );
       }
       
    }
    


    Regards, Phill W.

    Tuesday, October 01, 2013 12:29 PM
  • i have 2 errors 

    1) walker.Text no property Text

    2) GetFirstChild(el) el not exists

    3 ) LoadR(child) no overload


    Wacky Teseo

    Wednesday, October 02, 2013 7:53 AM
  • Hi Wacky,

    Phill provided a right way to get it resolved-- recursive function-- LoadR. I think this is the key Phill would like to show you. Generally, others just provide us an idea, and we need to understand and apply the idea with our code but not just copy and past.

    To avoid the errors you encountered, try below steps:

    1. remove the line: MessageBox.Show( walker.Text );

    2. Change LoadR to private void LoadR( TreeWalker walker,  AutomationElement el)

    3. Change all calls to LoadR to meet the new format of LoadR function.



    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, October 03, 2013 7:20 AM
  • I have Error in line red 


    Wacky Teseo

    Thursday, October 03, 2013 6:23 PM
  • Change the line to LoadR(walker, child);


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, October 04, 2013 2:37 AM
  • i have this error


    Wacky Teseo

    Friday, October 04, 2013 4:08 AM
  • I think you mentioned that code worked for you when you submited this question. You need to find out what happens to this, or submit a new question in the forum. Because it is not the question you asked in your original post.

    Thanks for your understanding.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, October 04, 2013 7:01 AM