By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,630 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

Specifying the parent when adding treeview nodes?

P: n/a
I'm using the EnumChildWindows API with an EnumChildWndProc callback
to populate the treeview. The output will be something similar to spy+
+

How can I specify the parent when adding a new node ??

When adding a new node is there any way to get an handle or something
else to be able add the childs to the correct parent ?

Thanks!
Nov 27 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 2007-11-27 13:30:35 -0800, SQACSharp <ls*********@hotmail.comsaid:
I'm using the EnumChildWindows API with an EnumChildWndProc callback
to populate the treeview. The output will be something similar to spy+
+

How can I specify the parent when adding a new node ??

When adding a new node is there any way to get an handle or something
else to be able add the childs to the correct parent ?
If I understand the question correctly, the fact that you are
enumerating windows is irrelevant to your question.

And again, assuming I understand the question correctly, the answer is
that you simply add the node to the Nodes collection of the node you
want to be the parent. For a root-level node, this will be the Nodes
collection of the TreeView itself, and for any other node, this will be
the Nodes collection of a TreeNode.

If that doesn't answer your question, you should rephrase your
question, explaining the relevance of the window enumeration if there
is any, and more specifically describing what it is you actually want
to do.

Pete

Nov 27 '07 #2

P: n/a
You can only get the Parent property on the TreeNode class. If you want
to add a TreeNode as a child of another TreeNode, you have to have a
reference to the parent TreeNode, and call the Add method on the Nodes
collection exposed by the parent.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"SQACSharp" <ls*********@hotmail.comwrote in message
news:9c**********************************@d4g2000p rg.googlegroups.com...
I'm using the EnumChildWindows API with an EnumChildWndProc callback
to populate the treeview. The output will be something similar to spy+
+

How can I specify the parent when adding a new node ??

When adding a new node is there any way to get an handle or something
else to be able add the childs to the correct parent ?

Thanks!

Nov 27 '07 #3

P: n/a
On 2007-11-27 15:01:07 -0800, SQACSharp <ls*********@hotmail.comsaid:
Sorry it's hard to explain for me, so maybe you will understand the
problem of enumerating child windows and adding the node to the
correct parent with an example :
Basically, you need to keep track of the node corresponding to the
parent. There are lots of ways you might do this, but in the code you
posted, it might look something like this (original code quoted, new
code unquoted):
[...]
foreach (string MyWindow in ListOfWindows)
{
TreeNode nodeParent = new TreeNode(MyWindow);
// Add a node for each windows associated to the processId
MyNode.Nodes.Add(nodeParent));
>
foreach (string child in
(ArrayList)ListOfChildWindows[WindowCounter])
{
//Add all the childs associated to the window

//*** PROBLEM BEGIN ***
//The following node must be added to the correct
parent..it's added to the same level as other windows for now
nodeParent.Nodes.Add("->"+child.ToString());
>
//*** PROBLEM END ***
}
WindowCounter++;
}
}
I am curious whether you care that you've only obtained a list one
level deep in the window hierarchy. Any window can have a child
window, and so you really ought to have some kind of recursive
operation that actually does enumerate every window of the process,
rather than just the non-child windows and their children.

But assuming that the one-level-deep is sufficient for your needs, the
above change should work for you.

Pete

Nov 27 '07 #4

P: n/a
Ok... I will try to explain the problem again :

EnumChildWindows return **ALL** child windows of a given handle. A
child window can also be a parent of another child and so on... So
when adding childs, the parent can be the main window or any other
child returned by enumChildWindow.

The only thing i know is the parent handle returned by Win32
GetParent. I'm trying to find a way to select the correct node with
the correct handle to add the child node.

Example of a window :
MainWindows1, Handle=12345
|----Label1, Handle=54321
|----GroupBox1 , Handle=11111
|---- CheckBox1, Handle=666 (the parent of checkbox1 is the
child groupbox1 and not the main window)

So when calling EnumChildWindows it return label1, groupbox1 and
checkbox1..... When adding the node for checkbox1 how to tell that the
node must be created under the node GroupBox1 (with the handle 11111)

The solution can be to use the key to store the handle of the
window... but i'm still trying to find the way to found the node with
a specific key and being able to add a child node under it.

Thanks again...
Nov 28 '07 #5

P: n/a
On 2007-11-27 17:23:47 -0800, SQACSharp <ls*********@hotmail.comsaid:
Ok... I will try to explain the problem again :

EnumChildWindows return **ALL** child windows of a given handle. A
child window can also be a parent of another child and so on... So
when adding childs, the parent can be the main window or any other
child returned by enumChildWindow.
Ah. For some reason I was thinking it behaved the same as GetWindow().
It's been awhile.

It didn't help that your original code didn't even put the child
windows under the thread node in the TreevView. It was a bit confusing
to try to understand what exactly you were trying to do.

Anyway, as far as the specific question goes...

One possible solution would be in fact to use GetWindow() to enumerate
the windows in the process. That way your enumeration could be done in
a recursive method that also tracks the current parent node so that it
could be handled appropriately.

Alternatively, you could store the TreeNode instances you create in a
Dictionary<IntPtr, TreeNode>, where the window handle corresponding to
the node is used for the key. Then when adding a TreeNode, you can use
GetParent() to get the window handle for the parent of the current
node's window handle, use that to look up the appropriate TreeNode and
add the current node to that node's Nodes collection.

There are actually lots of other ways to do this, assuming I finally
understand the question correctly. But I think the two methods above
are good representations of the two basic ideas you might use. That
is, either:

1) enumerate the child windows in a way such that you always know
the relationship of the current node in the enumeration to its parent.
This is likely to always involve some kind of recursion, whether
explicitly as a recursive method or using a stack data structure to
maintain the state of the enumeration.

2) retrieve the parent information from the current node, given the
window handle of the parent. A dictionary is an efficient way to do
this, but you could do a brute-force search of the TreeViews nodes if
you really wanted to.

If the above doesn't help, you'll have to try to elaborate again I think.

Pete

Nov 28 '07 #6

P: n/a
Thanks peter and Nicholas for trying to help.. I really appreciate
your help and thank you for your time!! :)

I found the solution after loosing all the day on this :

When adding the node I use the .Add function with two parameters (the
keys and the text). The window handle is stored in the key
parameter. Then, when adding my child node I simply use the .Find
function to search the parent node with the handle. The TreeNode
returned by the .Find function can be use to add nodes!
Nov 28 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.