Recreate a >>C# WinForms<< TreeView "state" -after- treeview rebuild ?? Any TreeView GODS out there

F

frostbb

I have a C# WinForms treeview with 20 or so 'level 0' nodes.

My users will normally have 2 of the 'level 0' branches open down to a '5th
level' selected node.

The users will make updates to the 2 selected nodes that will alter the node
name.

I think I need to rebuild the TreeView to get everything synchronized after
they click an update button.

Is there an example somewhere that shows how to recreate and re-expand level
0 branches of the TreeView state after the Tree has been rebuilt?

Put another way ... At the time the user commits an update the Level 0 nodes
5 and 18 are open 'down' to a pair of selected 5th level nodes. One node per
branch. All the other level 0 nodes are collapsed.

How do I rebuild the treeview and open the 5 and 18 level 0 nodes down to
the 5th level and reselect the 5th level nodes that the users had
previously selected and updated ?? I can save the names of the previously
selected and updated nodes. I'm just unsure of how to selectively expand
the 5 and 18 level 0 branches down to the 5th level and reselect the target
nodes.

Any help would be GREATLY appreciated.

Thanks in advance.

Barry in Oregon
 
J

Joel Lucsy

frostbb said:
How do I rebuild the treeview and open the 5 and 18 level 0 nodes down to
the 5th level and reselect the 5th level nodes that the users had
previously selected and updated ?? I can save the names of the previously
selected and updated nodes. I'm just unsure of how to selectively expand
the 5 and 18 level 0 branches down to the 5th level and reselect the target
nodes.

Save the state of ANY node that is expanded. When rebuilding the tree,
after inserting a node and inserting its children, check to see if its
one of your saved expanded nodes, and expand it.
For storing which items are expanded, I'd store the full path to the
node from the root.
 
P

Pavel Minaev

frostbb said:
I have a C# WinForms treeview with 20 or so 'level 0' nodes.

My users will normally have 2 of the 'level 0' branches open down to a '5th
level' selected node.

The users will make updates to the 2 selected nodes that will alter the node
name.

I think I need to rebuild the TreeView to get everything synchronized after
they click an update button.

Not necessarily. If you can track the specific changes that are
happening to the data source from which the tree is loaded, you can
avoid the full rebuild, instead just inserting, removing or moving the
nodes that were changed. And that will preserve the collapsed/expanded
state of all nodes.
 
Z

Zhi-Xin Ye [MSFT]

Dear Barry,

As I understand, you have a TreeView which has five levels of nodes, you
want to restore the expanded status of the TreeView after rebuilding the
TreeView.

We can expand a specify node by setting the TreeView.SelectedNode property
to the specified node.
In order to find the saved nodes, I would recommend you use unique names
for the TreeNodes, then you can call the TreeView.Nodes.Find() method to
find the nodes you saved by their names.

For example, we can do something like this:

(The variable "NodeKeys" in the code is an string array ,which stores the
names of the selected and update TreeNodes. )

private void button1_Click(object sender, EventArgs e)
{

//TODO: Rebuild the tree here

//Re-expand the nodes after rebuild the tree
foreach (string key in this.NodeKeys)
{
this.treeView1.SelectedNode =
this.treeView1.Nodes.Find(key, true)[0];
}
}

Please try my suggestion and let me know the result.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
F

frostbb

Thanks for the reply. Hadn't considered simply dropping and recreating the
existing nodes. In this case I may actully be able to get away with that!
I'm pretty sure I can get this thing to work using your and the other
replies to my question.

Best wishes. Happy computing and THANKS!

Barry in Oregon
 
F

frostbb

Zhi-Xin, (hopefully correct)

Thanks for the reply. EXAMPLES ARE ALWAYS APPRECIATED! :) Also appreciate
your time and knowledge. I'm pretty sure I can get this to work using your
and the other replies to my question.

Best wishes. Happy computing and THANKS!

Barry in Oregon
 
F

frostbb

Joel,

Thanks for the reply. Really appreciate your time and knowledge. I'm pretty
sure I can get this to work using your
and the other replies to my question.

Best wishes. Happy computing and THANKS!

Barry in Oregon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top