Results 1 to 11 of 11

Thread: Adding a node to a treeview on a different form...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    98

    Adding a node to a treeview on a different form...

    Okay, i have 2 forms. Im looking to create a seperate form that a user can enter the name of the node as. I have this

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            user = TextBox1.Text
            Dim newnode As TreeNode = New TreeNode(user)
            forms.myform1.TreeView1.SelectedNode.Nodes.Add("test")
            forms.myform2.Hide()
        End Sub
    the show/hide form2 works, but i get this for the treeview:
    Additional information: Object reference not set to an instance of an object.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I think it's making that error because you are creating the treeview node on the form1 , though you want it to be created on form2 for example . One solution (I've not tried this) I might think of is to create one or array of treeview nodes on form2 , when you need them , they are there . Just pass the string the user will enter . I dunno if this will work for you but it's worth a try .

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    98
    i want the node too be created on form one. form 2 is naming the node

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    You need to point to your instance of Form1. So that instance has to be accessilbe by Form2 either by having the instance as a global variable or passing the instance to Form2 via the new method.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    98
    Yes, ive tried that, but that means i have to show a new form 1

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The easiest and best way is to either have the child form return a treenode to the main form for call a method on the main form that does the actual creating. You could do this via a delegate or just a public method.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    98
    hmmm... could you explain that a little bit? im still a bit new to .net

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is an example of both ways with explanation in the code notes.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    98
    hmmm... i cant seem to open it in vb, i have unzipped it

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I think you might need VS.NET 2003, since I think thats what Edneesis is using.

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes that is what I am using, but if you have VS 2002 then you can convert it back to 2002. Someone has a link to the converter in their signature, Pirate maybe?

    Yes its in his signature as 'Utility to Convert Visual Studio.NET 2003 Project Files' or here I'll cut and paste it for ya:

    http://www.codeproject.com/useritems/VSConvert.asp

    There isn't anything in the project that will be different in 2002.

Posting Permissions

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



Click Here to Expand Forum to Full Width