VS 2015 Code fails when called from different controls-VBForums
Results 1 to 5 of 5

Thread: Code fails when called from different controls

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2016
    Location
    UK
    Posts
    19

    Code fails when called from different controls

    Hi im coming to the end of a project that's a file management program that works with images and im getting some very strange errors. In my program im used a subclassed treeview and subclassed tree nodes. Everything has been working great with the new classes until today when just one of them has stopped working.

    The process is you click a button from a toolstrip and the subclassed treeview gathers up some image files and displays them using my subclassed treenodes. The if the user clicks on one of the nodes that displays the image files the action is picked up by the treeviews 'AfterSelect' event and then passed onto a picture box for display. But for some reason today ive been getting an System.InvalidCastException from the custom treenode in 'AfterSelect'. This is odd because theres lots of methods that are using the same subclassed tree nodes without any error and up until today the code in 'AfterSelect' has been working fine. So to hunt down the problem i rebuilt the subclassed treeview based around a small bit of example code from Microsoft . I tested it out on a different form and it worked fine. I then moved it over to my main form and tested it there but with a standard button to trigger the sequence rather than from the tool strip i want to use. It worked fine there as well. So then i took the code from the standard button and attached it to the toolstrip button and i got the System.InvalidCastException again. I even tried adding a different button to the toolstrip and triggering the sequence from there and it still gives the same error.

    Code:
        Private Sub SourceToolStripAdd_Click(sender As Object, e As EventArgs) Handles SourceToolStripAdd.Click
            XTreeView1.Grab_Some_files()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            XTreeView1.Grab_Some_files()
        End Sub
    
        Private Sub XTreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles XTreeView1.AfterSelect
            Dim mynode As xNodeInput
            mynode = CType(e.Node, xNodeInput)
            MessageBox.Show("Node selected is " & mynode.nxText_SourceFile)
        End Sub
    I added the line
    Code:
    MyBase.New()
    in the 'public sub new' on the subclassed treenode as Microsoft suggested but it has not helped

    I cant think what might cause this.
    Last edited by LucasCain; Jun 26th, 2017 at 09:31 PM. Reason: more detail

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,539

    Re: Code fails when called from different controls

    I assume that you're saying, without actually saying, that the invalid cast is on this line:
    vb.net Code:
    1. mynode = CType(e.Node, xNodeInput)
    In that case, the node that was selected is not type xNodeInput. Presumably it is simply type TreeNode, which you can check quite easily for yourself. In that case, you need to go back to where the nodes were created to see why TreeNode objects are being created instead of xNodeInput objects.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2016
    Location
    UK
    Posts
    19

    Re: Code fails when called from different controls

    Hi jmcilhinney thanks for the quick reply,I really wasnt sure id be able to explain it well enough for anyone to help.
    .
    i know right away im using standard treenodes for parents and my xNodeInput nodes for children. I will modify the code and get back to you, my moneys on your suggestion.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2016
    Location
    UK
    Posts
    19

    Re: Code fails when called from different controls

    Ezxcellent you were right first time, thanks it would have took me ages to getting round to finding that out.

    I fixed the problem with this bit of code in 'after select'
    Code:
            Dim mynode As xNodeInput
            Dim typ As Type = e.Node.GetType ' gets the type of object sent
    
            If typ.Equals(GetType(xNodeInput)) Then
                mynode = CType(e.Node, xNodeInput)
                MessageBox.Show("Node selected is " & mynode.nxText_SourceFile)
            End If

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,539

    Re: Code fails when called from different controls

    I would tend to use TryCast instead:
    vb.net Code:
    1. Dim myNode = TryCast(e.Node, xNodeInput)
    2.  
    3. If myNode IsNot Nothing Then
    4.     '...
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.