When using numericupdown to expand/collapse treeView nodes. Why the collapse part is not working ?

I have a treeView in my form1 designer. The treeView variable name is: treeViewMS1
When i'm running my program the treeView is automatic expanded to level 1:
Now if i click on the numericUpDown and change the value to 2 then:
So the expanded part is working fine when i change of the numericUpDown by one up the expanded is working fine.
Now when it's on level 2 and i change the numericUpDown back to value 1 that's level 1 instead get back to my first screenshot Expanded level 1 it's getting back to the root level 0.
and i want that the collapse part will move only one level back but it dosen't matter if i'm on expanded level 2 or 3 or 5 it will allways jump to 0 to the root.
This is the numericUpDown value changed event:
decimal oldValue;
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
if (numericUpDown1.Value > oldValue)
ExpandToLevel(treeViewMS1.Nodes, (int)numericUpDown1.Value);
else
CollapseToLevel(treeViewMS1.Nodes, (int)numericUpDown1.Value);
oldValue = numericUpDown1.Value;
ExpandToLevel method:
void ExpandToLevel(TreeNodeCollection nodes, int level)
if (level > 0)
foreach (TreeNode node in nodes)
node.Expand();
ExpandToLevel(node.Nodes, level -1);
And CollapseToLevel method:
void CollapseToLevel(TreeNodeCollection nodes, int level)
if (level > 0)
foreach (TreeNode node in nodes)
node.Collapse();
CollapseToLevel(node.Nodes, level - 1);

I solved it this way:
I solved it like this: In the Form1_Load event i did:
SetToLevel(treeViewMS1.Nodes, 1);
In my case i wanted it to begin by default in level 1.
Then in the numericupdown1 changed value event:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
SetToLevel(treeViewMS1.Nodes, (int)numericUpDown1.Value);
Then the method SetToLevel:
void SetToLevel(TreeNodeCollection nodes, int level)
foreach (TreeNode node in nodes)
node.Collapse(false);
ExpandToLevel(nodes, level);
And last the method EXpandToLevel:
void ExpandToLevel(TreeNodeCollection nodes, int level)
if (level > 0)
foreach (TreeNode node in nodes)
node.Expand();
ExpandToLevel(node.Nodes, level -1);
And now it's working perfect like i wanted it to work. When changing the numericupdown value it's changing the node tree expand/collapse levels.

Similar Messages

Maybe you are looking for

  • No Template Found while creating Asset

    As admin I have created new template, flex definition. As a contributor when trying to create Flex Asset, it is not showing any Template. Gives a message 'No template found. Why it doesn't take the Template created.

  • About Flash P2P live streaming from non-webcam sources

    Hello, I am a university student. Our lab is attempting to work on a p2p live streaming using flash p2p features. The media source is not from a webcam but a file from a certain server, which is not directly supported by any of the 4 methods flash pl

  • Can not start iAS sp3

    I am having a problem starting the app server with the following messages in ias.log: kas> error: init of context failed! iPlanet Application Server is running in international mode GXBindInit: GXBindBasic failed GXContextInit: GXBindInit failed 2 ka

  • I can't install or buy some apps after opting out from Family sharing

    I created family sharing and used it with my brother. Then I dismissed it. When I try to install or buy apps, which previously were bought by him, appstore requires to enter a password from my brother's icloud. Even if I install a free app.

  • Regarding  payment term

    Payment term should be: Payable with next payment run, max 14 days from Document Date. Can you please let me know the possibility of creating such a payment term? thanks venkat