Results 1 to 5 of 5

Thread: Treeview.Tag property

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    58

    Treeview.Tag property

    I am reading through a datatable and setting the text property of the node to a name, and the tag property to a Guid. I want to get both values but I keep getting an error (Object Reference not set to an instance of an object) on the tag property. What is the proper way to get the value from the tag property.
    string s = e.Node.Text;
    string t = e.Node.Tag.ToString();

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Treeview.Tag property

    Your thread title says "TreeView.Tag property", not "TreeNode.Tag property". Are you sure that you're assigning the GUID to the node's Tag property in the first place and not the tree's Tag property? Your error message indicates that that is a possibility.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Treeview.Tag property

    I think you may have it there John. If the error says "Object not set to instance blah..." then you need to either step through your code looking to see if its going through your code to set the nodes tag property or place a breakpoint on the code that is setting the nodes tag property and see if it gets executed.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    58

    Re: Treeview.Tag property

    Thanks for the quick reply

    what im doing is

    node = treeConsumers.Nodes.Add("ID's");

    foreach (DataRow dr in dtExport.Rows)
    {
    node.Nodes.Add(dr["Last_Name"].ToString().Trim()
    node.Tag = dr["UserGUID"]
    }

    I guess that would set the Tag to the parent node. How do I set it on the child nodes then??

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Treeview.Tag property

    As you can see from your first line, the Add method returns a reference to the node you just added. That means that in the loop the Add method will return a reference to the child node you just added, so you can set its Tag directly:
    Code:
    node.Nodes.Add(dr["Last_Name"].ToString().Trim()).Tag = dr["UserGUID"];

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