Tree view node selection error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Honek

    #1

    Tree view node selection error

    Hi,

    I have the code below that I'm using in a tree view to detect what node the
    user has clicked on.

    It compiles but on runtime I get a type cast error.

    I've used this method for both the tool bar and menu and it works fine
    extending the "handles" property inherent within .net.

    Isn't the type supposed to be "TreeNode"?



    Private Sub tvSelectFolder_ NodeMouseClick( ByVal sender As Object, ByVal e As
    System.Windows. Forms.TreeNodeM ouseClickEventA rgs) Handles
    tvSelectFolder. NodeMouseClick

    'Declare an object of type TreeNode

    Dim pTreeViewNode As TreeNode

    'Now let's find out which tree view node was clicked

    pTreeViewNode = CType(sender, TreeNode)



    'Each item corresponds to a different feature

    Select Case pTreeViewNode.N ame

    '//Exit the program

    Case "InboxNode"

    End Select

    End Sub

    End Class



    Thanks,

    Adam


  • Cor Ligthert [MVP]

    #2
    Re: Tree view node selection error

    Adam,

    Did you set (as would be done as first if you have a casting problem) set in
    top of your program or in the options: Option Strict On

    That helps mostly to find casting errors.

    Cor

    "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> schreef in bericht
    news:%23X%23pLh PXGHA.3800@TK2M SFTNGP03.phx.gb l...[color=blue]
    > Hi,
    >
    > I have the code below that I'm using in a tree view to detect what node
    > the user has clicked on.
    >
    > It compiles but on runtime I get a type cast error.
    >
    > I've used this method for both the tool bar and menu and it works fine
    > extending the "handles" property inherent within .net.
    >
    > Isn't the type supposed to be "TreeNode"?
    >
    >
    >
    > Private Sub tvSelectFolder_ NodeMouseClick( ByVal sender As Object, ByVal e
    > As System.Windows. Forms.TreeNodeM ouseClickEventA rgs) Handles
    > tvSelectFolder. NodeMouseClick
    >
    > 'Declare an object of type TreeNode
    >
    > Dim pTreeViewNode As TreeNode
    >
    > 'Now let's find out which tree view node was clicked
    >
    > pTreeViewNode = CType(sender, TreeNode)
    >
    >
    >
    > 'Each item corresponds to a different feature
    >
    > Select Case pTreeViewNode.N ame
    >
    > '//Exit the program
    >
    > Case "InboxNode"
    >
    > End Select
    >
    > End Sub
    >
    > End Class
    >
    >
    >
    > Thanks,
    >
    > Adam
    >
    >[/color]


    Comment

    • Claes Bergefall

      #3
      Re: Tree view node selection error

      I would guess that the sender argument is actually the TreeView itself
      Use e.Node to get the clicked node

      /claes

      "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> wrote in message
      news:%23X%23pLh PXGHA.3800@TK2M SFTNGP03.phx.gb l...[color=blue]
      > Hi,
      >
      > I have the code below that I'm using in a tree view to detect what node
      > the user has clicked on.
      >
      > It compiles but on runtime I get a type cast error.
      >
      > I've used this method for both the tool bar and menu and it works fine
      > extending the "handles" property inherent within .net.
      >
      > Isn't the type supposed to be "TreeNode"?
      >
      >
      >
      > Private Sub tvSelectFolder_ NodeMouseClick( ByVal sender As Object, ByVal e
      > As System.Windows. Forms.TreeNodeM ouseClickEventA rgs) Handles
      > tvSelectFolder. NodeMouseClick
      >
      > 'Declare an object of type TreeNode
      >
      > Dim pTreeViewNode As TreeNode
      >
      > 'Now let's find out which tree view node was clicked
      >
      > pTreeViewNode = CType(sender, TreeNode)
      >
      >
      >
      > 'Each item corresponds to a different feature
      >
      > Select Case pTreeViewNode.N ame
      >
      > '//Exit the program
      >
      > Case "InboxNode"
      >
      > End Select
      >
      > End Sub
      >
      > End Class
      >
      >
      >
      > Thanks,
      >
      > Adam
      >
      >[/color]


      Comment

      • Adam Honek

        #4
        Re: Tree view node selection error

        Thanks, this does the trick using the "e" argument.

        BTW, Option Strict was off.

        Adam

        "Claes Bergefall" <louplou@nospam .nospam> wrote in message
        news:ODsVvyWXGH A.4976@TK2MSFTN GP03.phx.gbl...[color=blue]
        >I would guess that the sender argument is actually the TreeView itself
        > Use e.Node to get the clicked node
        >
        > /claes
        >
        > "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> wrote in message
        > news:%23X%23pLh PXGHA.3800@TK2M SFTNGP03.phx.gb l...[color=green]
        >> Hi,
        >>
        >> I have the code below that I'm using in a tree view to detect what node
        >> the user has clicked on.
        >>
        >> It compiles but on runtime I get a type cast error.
        >>
        >> I've used this method for both the tool bar and menu and it works fine
        >> extending the "handles" property inherent within .net.
        >>
        >> Isn't the type supposed to be "TreeNode"?
        >>
        >>
        >>
        >> Private Sub tvSelectFolder_ NodeMouseClick( ByVal sender As Object, ByVal e
        >> As System.Windows. Forms.TreeNodeM ouseClickEventA rgs) Handles
        >> tvSelectFolder. NodeMouseClick
        >>
        >> 'Declare an object of type TreeNode
        >>
        >> Dim pTreeViewNode As TreeNode
        >>
        >> 'Now let's find out which tree view node was clicked
        >>
        >> pTreeViewNode = CType(sender, TreeNode)
        >>
        >>
        >>
        >> 'Each item corresponds to a different feature
        >>
        >> Select Case pTreeViewNode.N ame
        >>
        >> '//Exit the program
        >>
        >> Case "InboxNode"
        >>
        >> End Select
        >>
        >> End Sub
        >>
        >> End Class
        >>
        >>
        >>
        >> Thanks,
        >>
        >> Adam
        >>
        >>[/color]
        >
        >[/color]


        Comment

        Working...