Jump to content

windows.form.treeview control question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Using C#

I want to know if it is possible to limit the user to only being able
to see the path of the current node.


In other words I dont wan't the user to be able to see any expanded
stuff that isn't part of the path of the selected node.


So I need some kind of function that will CollapseAll() except the part
of the tree the user is currently working with...


If this is possible does anyone have any ideas to point me in the right
dirrection?
:confused:

Thanks.

#2
RobSoftware

RobSoftware

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
Let me see if I understand you (in ascii tree-view layout, lol):

Tree Root
- 2nd Node
-- 2nd Node, 1st Child Node
- 3rd Node
- - 3rd Node, 1st Child Node
- 4th Node

Now, you want everything non-visible except the one they are working in, such as 3rd Node, 1st Node in the tree above? So, do you want all other nodes to be Collapsed or do you want nothing to be seen except 3rd Node, 1st Node?

A) Do you want them to see just
3rd Node, First Child Node

OR

B)
Tree Root
- 2nd Node
- 3rd Node
- - 3rd Node, 1st Child Node // Working node
- 4th Node


.... I'm assuming B?

#3
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
It looks like there's a CollapseAll method on TreeView, so my initial thought would be to hook up an event handler to the TreeView.BeforeExpand event and call TreeView.CollapseAll, and then let it expand.

#4
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
A) Do you want them to see just
3rd Node, First Child Node

OR

B)
Tree Root
- 2nd Node
- 3rd Node
- - 3rd Node, 1st Child Node // Working node
- 4th Node


.... I'm assuming B?[/QUOTE

Yep.

I'm thinking I need to use the depth property but I'm not sure... maybe theres another way?

#5
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
THanks guys but I figured this one out... Here's what I did.

TreeNode objNode = (TreeNode)e.Node;

treeView1.CollapseAll();

if (!objNode.IsExpanded)

{

objNode.EnsureVisible();

}

Pretty simple actually:D

#6
RobSoftware

RobSoftware

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
Ahh. I was about to load up C# to figure this one out! So that code there keeps the one the user is working on expanded?

Does it collapse the one the user was working on?

#7
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Every time a node is selected the treeview collapses and expands only what it needs to display the node that was selected.

Now I have another problem, everytime this happens the treeview "flickers" I don't think this is acceptable, is there some way to prevent this?

#8
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Figured this one out too....

just stick a treeView1.Update(); after the if statement and the problem is solved.

#9
RobSoftware

RobSoftware

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
The update will cause it not to flicker?

#10
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
The Update function stopped the flickering, but after testing it further it still wasn't stopping all the visual defects caused by the collapsing and expanding of nodes. So I had to revise the code a little,and this works like a charm;)


TreeNode objNode = (TreeNode)e.Node;

				

			treeView1.BeginUpdate();		  

			treeView1.CollapseAll();

			if (!objNode.IsExpanded)

				objNode.EnsureVisible();


			treeView1.EndUpdate();



#11
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
and to explain, the beginupdate() function lets the program know that no visual changes should be made to the treeview until the endupdate() function is called. This prevents the treeview from showing nodes collapsing and expanding which isn't noticable if you're only dealing with a few nodes.

#12
Crane

Crane

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 398 posts
Very nice code hoser. I never knew about the treeview update feature.