treeview selected node

A

auntiejack56

Hi, I've got a treeview, and I want to display the node text property
that I have

a) clicked on

or

b) highlighted using the up or down arrows


Could anyone please tell me how to do this? I guess there are two
separate events involved? If I use After Select, the form errors out
on opening.

thanks,

Ray
 
A

Armin Zingler

auntiejack56 said:
Hi, I've got a treeview, and I want to display the node text
property that I have

a) clicked on

or

b) highlighted using the up or down arrows


Could anyone please tell me how to do this? I guess there are two
separate events involved? If I use After Select, the form errors out
on opening.

AfterSelect event is the right place. Which error do you get in which
line?


Armin
 
P

Phill W.

auntiejack56 said:
I've got a treeview, and I want to display the node text property
that I have (a) clicked on or (b) highlighted using the up or down arrows

[TreeView].SelectedNode will get you the currently selected Node /if/
there /is/ one.

If not, you'll get a null reference, which your code has to deal with.
I guess there are two separate events involved?

No; it's the same event, AfterSelect, because in both cases, you're
selecting a Node; the TreeView control doesn't care /how/ you chose to
do so.
If I use AfterSelect, the form errors out on opening.

Presumably with a NullReferenceException?
If there isn't a currently selected Node, you have to deal with this by
testing for Nothing first. That said, does AfterSelect even fire until
you select a Node? I can't remember. Post the code that's failing and
we'll have a look.

HTH,
Phill W.
 
A

auntiejack56

Well do we really have to tell you that the error message could help ?Thanks, first I just wanted to know that I was coding to the right
event. Then indeed the error message helps.

What I've put in is:

Private Sub scrTree_AfterSelect(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles scrTree.AfterSelect
Dim tTree As TreeView = sender
If Not tTree.SelectedNode Is Nothing Then
MessageBox.Show("Clicked on " & tTree.SelectedNode.Text)
End If
End Sub

Which works fine, thanks all for your help.

I have a couple of other questions though, sorry to plague you:

When I comment out the messagebox line, the form with the treeview
opens as expected (obviously with no message).

When I uncomment the code, the messagebox pops up before the form
displays. All good so far. I click yes. The form doesn't display. It's
there, but I have to click on the taskbar to get it to show. I have a
feeling that I'm breaching some norm of form opening in VB?

Question 2 is, should I be using the local variable declaration to
pick up the treeview object from 'sender' or is there a better or more
appropriate way?

Thanks - Jack
 
P

Phill W.

auntiejack56 said:
Private Sub scrTree_AfterSelect( _
ByVal sender As Object _
, ByVal e As System.Windows.Forms.TreeViewEventArgs _
) Handles scrTree.AfterSelect
Dim tTree As TreeView = sender
If Not tTree.SelectedNode Is Nothing Then
MessageBox.Show("Clicked on " & tTree.SelectedNode.Text)
End If
End Sub

You're not using "Option Strict On" and I would strongly recommend that
you should. The statement ...

Dim tTree As TreeView = sender

.... wouldn't compile because you're implicitly casting an "Object"
(that's any old object) into a TreeView. There's /no/ guarantee that
you're always going to get a TreeView calling this routine (I know; it's
a TreeView event handler, but this is the Brave New World of event
handling in .Net).

Dim tTree as TreeView = Nothing
If TypeOf sender Is TreeView Then
tTree = DirectCast( sender, TreeView )
. . .
End If

(You might want to test for Nothing in there as well, to be really,
/really/ paranoid).
When I uncomment the code, the messagebox pops up before the form
displays. All good so far. I click yes. The form doesn't display. It's
there, but I have to click on the taskbar to get it to show. I have a
feeling that I'm breaching some norm of form opening in VB?

It's a /bit/ unusual to be popping up a dialog from the Load processing
of a Form, but that still /shouldn't/ cause the Form to be minimised as
a result.
Question 2 is, should I be using the local variable declaration to
pick up the treeview object from 'sender' or is there a better or more
appropriate way?

No, that's fine.
You can avoid [coding] the variable using a "With" block ...

With DirectCast( sender, TreeView )
. . .
End With

.... but I /think/ the compiler just creates a variable for itself, so
you're not gaining anything.

And, before you ask, I prefer DirectCast over CType because DirectCast
/only/ does "pointer casting"; CType /can/ do more for you, largely when
you don't expect it (and, probably, don't want it to. YMMV).

HTH,
Phill W.
 
J

Jack Jackson

Dim tTree as TreeView = Nothing
If TypeOf sender Is TreeView Then
tTree = DirectCast( sender, TreeView )
. . .
End If

I usually do it this way because it involves less typing:
Dim tTree as TreeView = TryCast(sender, TreeView)
If tTree IsNot Nothing Then
. . .
End If

Is there any reason to prefer one over the other?
 
P

Phill W.

I usually do it this way because it involves less typing:
Is there any reason to prefer one over the other?

I wouldn't have thought so, no.

TryCast probably uses TypeOf and DirectCast under the covers anyway (or
TypeOf and /CType/ which, IIRC, in turn uses DirectCast).

Regards,
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