Treeview doubleclick help

J

John Dann

I'm trying to implement a fairly simple Treeview procedure but running
into problems.

My Treeview has just two levels of nodes: top-level and one child
level. I want a doube-click on the Treeview to do one of two things:

If the double-click was on a top-level node then the default action of
exapnding/collapsing the node should be allowed as usual and with no
further action.

If the double-click was on a child-node then I want to be able to pick
up the text of the child item that was clicked (ie the selected item).

(And if the double-click was on neither then just ignore)

The problem is how best to distinguish between the top- and
child-level clicks.

I'm obviously looking to place some code in the Treeview's doubleclick
event but the eventargs of this event don't AFAICS return anything
useful.

Any pointers much appreciated.

JGD
 
T

tomb

You can include a key for each node, which can be any string you
choose. I did this in a project, which looked something like this:
level-name-creationdate-uniqueid

so every one was unique ( it is a key ). You can configure this however
suits you.

Tom
 
R

rowe_newsgroups

You could also maintain a collection that would contain just the root
nodes (add them to the collection when you populate the treeview
nodes). Then you can do a quick if RootNodeCollection.Contains(...)
test.

Just my 2 cents

Thanks,

Seth Rowe
 
G

Guest

In the click event, you can check to see if the node passed to the event has
a parent or not...if not, then it's not a child node.
 
P

Phill W.

John said:
I'm trying to implement a fairly simple Treeview procedure but running
into problems. My Treeview has just two levels of nodes: top-level
and one child level.

The problem is how best to distinguish between the top- and
child-level clicks.

Root nodes don't have a Parent whereas Child nodes do.

Check the TreeView's SelectedNode property and then whether or not it's
Parent property has any value.

Sub tv_DoubleClick( ... ) Handles tv.DoubleClick
If Not ( tv.SelectedNode Is Nothing ) Then
If tv.SelectedNode.Parent Is Nothing Then
' Root Node
Else
' Child Node (somewhere down the tree)
End If
Else
' Nothing at all selected
End If
End Sub

You /might/ also need to put code into the MouseUp event to ensure that
there is an item selected when the double-click event arrives. I can't
remember if this happens automatically or not, but I've had to do this
for context menus, so I thought I'd better throw it in :

Sub tv_MouseUp( ... ) Handles tv.MouseUp
Dim node as TreeNode _
tv.GetNodeAt( e.X, e.Y )

If Not ( node Is Nothing ) Then
tv.SelectedNode = node
End If
End Sub

If you take this model further, so that different nodes can perform
different "actions", then you might consider adding customised TreeNodes
to the Treeview and adding code into methods on these custom Treenodes,
something like :

Class BeepingNode
Inherits TreeNode

Public Sub New()
MyBase.New()
End Sub

Public Sub Beep()
Beep()
End Sub

End Class

Then

Sub tv_AfterSelect( ... ) Handles tv.AfterSelect
If TypeOf e.Node Is BeepingNode Then
DirectCast( e.Node, BeepingNode ).Beep()
End If
End Sub

HTH,
Phill W.
 

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