Code check needed

teamdad

Member
Joined
Jun 14, 2004
Messages
12
Programming Experience
Beginner
My code below will debug without errors, it just doesn't show the msgbox's like it should when the user clicks on the specific nodes. Anyone know how to get this fixed? I'm new to .NET and sorry if this is a simple answer/fix but I need to know what to do.

VB.NET:
Public Class Form1
    Dim Tc3p1 As TreeNode
    Dim Tc3p2 As TreeNode
    Dim Tc3p3 As TreeNode
    Dim Tc3p4 As TreeNode
    Dim Tc3p5 As TreeNode
    Dim n As TreeNode
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        n = sender.selectednode
        If n.Name Is Tc3p1 Then
            MsgBox("Click 1")
        End If
        If n.Name Is Tc3p2 Then
            MsgBox("Click 2")
        End If
        If n.Name Is Tc3p3 Then
            MsgBox("Click 3")
        End If
        If n.Name Is Tc3p4 Then
            MsgBox("Click 4")
        End If
        If n.Name Is Tc3p5 Then
            MsgBox("Click 5")
        End If
    End Sub 'TreeView1_AfterSelect
End Class

Thanks in advance.
 
I believe I already answered that question here. :D
Now you compare the Name property of a node with a node. As they are different types (System.String vs. TreeNode) the Is operator will always return False.
The simple fix you ask for is obviously something like
VB.NET:
       if e.Node Is Tc3p1 then MessageBox.Show("whatever")

But in fact I doubt you really need the variables ( Tc3p1, Tc3p2, ...) .
What exactly are you trying to achieve?
 
Last edited:
Hard to explain what i'm trying to do.... but use a treeview as like a main menu of sorts. Depending on what node on the treeview the user clicks it will do some other event in the program. It's not the traditional treeview that shows files etc...
 
@JuggaloBrotha: The selected node is already in the parameter e.Node, so the code could read (as it does in the link in my previous post):
VB.NET:
if e.Node is Tc3p1 then ...

@teamdad: Same as above: you get the node in the parameter. So you just have to find a way to connect the desired action to a node. There are several ways I can think of to achieve this automagically (probably are a lot of other ways):
a) abuse the TreeNode.Tag property and put a delegate in it
b) make your own derived TreeNode and expand it with your own Action property and put a delegate in your Action property
c) use a or b, but instead of a delegate use your own Command object
d) make your own derived TreeNode and expand it with an Event that triggers when the TreeNode is selected.
e) and so on ...
 
Don Delegate said:
@JuggaloBrotha: The selected node is already in the parameter e.Node, so the code could read (as it does in the link in my previous post):
VB.NET:
if e.Node is Tc3p1 then ...

i didnt know that, now i do :) (i've never use the treeview control yet lol )
 
I see all kind of errors and added complexity in your code... I am not sure what you are trying to do either. For one... with the code provided your looking at the name of the selected tree node vs a tree node... it needs to be compared to the name of the other tree node as well. Name vs Name also... if these treenodes are generic and your not distinguishing them you will get a msgbox for each. node vs node. Also, I would use Select case rather than so many if statements. And finally if your clicking on a tree node it is already declared and you should be checking against it's information not the info of those declared Tc3P1's.....

If you want some help with this for example you don't know what I am talking about then let's get together on some e-mails and I can teach you real quick. I've been through the same problems and I am self taught so I have sat mind boiling for a long time to get this and now I can share it.
 
Back
Top