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.