Value of tag property disappears












1















I'm busy with a simple application. It reads xml and puts the information in a treeview.



I do this by creating TreeNodes and nest them, and finaly, return the root treenode. Because I want to show some extra information when a treenode is selected, I put the information in the tag property of the TreeNode. In this way, I should be able to retrieve the information when the node is selected.



But when I try to retrieve the information in the Tag property, it says the value = null.



Here is the code where I fill the tag. This is in a function which is recursively used to read the XML dom. treeNode is a paramater given to this function.



if (treeNode.Tag == null)
{
treeNode.Tag = new List<AttributePair>();
}
(treeNode.Tag as List<AttributePair>).Add(new AttributePair(currentNode.Name, currentNode.Value));


This is the event where a treenode is selected



private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvXML.SelectedNode.Tag != null)
{
}
if (e.Node.Tag != null)
{
}
}


Both values evaluate to null. How can I solve this problem?










share|improve this question




















  • 1





    This works for me - may sound stupid, but are you sure you're not changing the nodes somewhere to set the Tag to null?

    – Thorsten Dittmar
    Aug 18 '09 at 9:02











  • You have to use the debugger and step into the code to see what actually happens

    – Ahmed Said
    Aug 18 '09 at 9:04











  • In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.

    – Ikke
    Aug 18 '09 at 9:14











  • Do you by chance have the Localizable property set on the form?

    – Filip Navara
    Aug 21 '09 at 17:55











  • Due to lack of time, failed to give more details. Sorry.

    – Ikke
    Aug 27 '09 at 17:31
















1















I'm busy with a simple application. It reads xml and puts the information in a treeview.



I do this by creating TreeNodes and nest them, and finaly, return the root treenode. Because I want to show some extra information when a treenode is selected, I put the information in the tag property of the TreeNode. In this way, I should be able to retrieve the information when the node is selected.



But when I try to retrieve the information in the Tag property, it says the value = null.



Here is the code where I fill the tag. This is in a function which is recursively used to read the XML dom. treeNode is a paramater given to this function.



if (treeNode.Tag == null)
{
treeNode.Tag = new List<AttributePair>();
}
(treeNode.Tag as List<AttributePair>).Add(new AttributePair(currentNode.Name, currentNode.Value));


This is the event where a treenode is selected



private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvXML.SelectedNode.Tag != null)
{
}
if (e.Node.Tag != null)
{
}
}


Both values evaluate to null. How can I solve this problem?










share|improve this question




















  • 1





    This works for me - may sound stupid, but are you sure you're not changing the nodes somewhere to set the Tag to null?

    – Thorsten Dittmar
    Aug 18 '09 at 9:02











  • You have to use the debugger and step into the code to see what actually happens

    – Ahmed Said
    Aug 18 '09 at 9:04











  • In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.

    – Ikke
    Aug 18 '09 at 9:14











  • Do you by chance have the Localizable property set on the form?

    – Filip Navara
    Aug 21 '09 at 17:55











  • Due to lack of time, failed to give more details. Sorry.

    – Ikke
    Aug 27 '09 at 17:31














1












1








1


1






I'm busy with a simple application. It reads xml and puts the information in a treeview.



I do this by creating TreeNodes and nest them, and finaly, return the root treenode. Because I want to show some extra information when a treenode is selected, I put the information in the tag property of the TreeNode. In this way, I should be able to retrieve the information when the node is selected.



But when I try to retrieve the information in the Tag property, it says the value = null.



Here is the code where I fill the tag. This is in a function which is recursively used to read the XML dom. treeNode is a paramater given to this function.



if (treeNode.Tag == null)
{
treeNode.Tag = new List<AttributePair>();
}
(treeNode.Tag as List<AttributePair>).Add(new AttributePair(currentNode.Name, currentNode.Value));


This is the event where a treenode is selected



private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvXML.SelectedNode.Tag != null)
{
}
if (e.Node.Tag != null)
{
}
}


Both values evaluate to null. How can I solve this problem?










share|improve this question
















I'm busy with a simple application. It reads xml and puts the information in a treeview.



I do this by creating TreeNodes and nest them, and finaly, return the root treenode. Because I want to show some extra information when a treenode is selected, I put the information in the tag property of the TreeNode. In this way, I should be able to retrieve the information when the node is selected.



But when I try to retrieve the information in the Tag property, it says the value = null.



Here is the code where I fill the tag. This is in a function which is recursively used to read the XML dom. treeNode is a paramater given to this function.



if (treeNode.Tag == null)
{
treeNode.Tag = new List<AttributePair>();
}
(treeNode.Tag as List<AttributePair>).Add(new AttributePair(currentNode.Name, currentNode.Value));


This is the event where a treenode is selected



private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvXML.SelectedNode.Tag != null)
{
}
if (e.Node.Tag != null)
{
}
}


Both values evaluate to null. How can I solve this problem?







c# winforms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 2:48









Cœur

17.6k9104145




17.6k9104145










asked Aug 18 '09 at 8:58









IkkeIkke

71.5k2281108




71.5k2281108








  • 1





    This works for me - may sound stupid, but are you sure you're not changing the nodes somewhere to set the Tag to null?

    – Thorsten Dittmar
    Aug 18 '09 at 9:02











  • You have to use the debugger and step into the code to see what actually happens

    – Ahmed Said
    Aug 18 '09 at 9:04











  • In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.

    – Ikke
    Aug 18 '09 at 9:14











  • Do you by chance have the Localizable property set on the form?

    – Filip Navara
    Aug 21 '09 at 17:55











  • Due to lack of time, failed to give more details. Sorry.

    – Ikke
    Aug 27 '09 at 17:31














  • 1





    This works for me - may sound stupid, but are you sure you're not changing the nodes somewhere to set the Tag to null?

    – Thorsten Dittmar
    Aug 18 '09 at 9:02











  • You have to use the debugger and step into the code to see what actually happens

    – Ahmed Said
    Aug 18 '09 at 9:04











  • In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.

    – Ikke
    Aug 18 '09 at 9:14











  • Do you by chance have the Localizable property set on the form?

    – Filip Navara
    Aug 21 '09 at 17:55











  • Due to lack of time, failed to give more details. Sorry.

    – Ikke
    Aug 27 '09 at 17:31








1




1





This works for me - may sound stupid, but are you sure you're not changing the nodes somewhere to set the Tag to null?

– Thorsten Dittmar
Aug 18 '09 at 9:02





This works for me - may sound stupid, but are you sure you're not changing the nodes somewhere to set the Tag to null?

– Thorsten Dittmar
Aug 18 '09 at 9:02













You have to use the debugger and step into the code to see what actually happens

– Ahmed Said
Aug 18 '09 at 9:04





You have to use the debugger and step into the code to see what actually happens

– Ahmed Said
Aug 18 '09 at 9:04













In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.

– Ikke
Aug 18 '09 at 9:14





In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.

– Ikke
Aug 18 '09 at 9:14













Do you by chance have the Localizable property set on the form?

– Filip Navara
Aug 21 '09 at 17:55





Do you by chance have the Localizable property set on the form?

– Filip Navara
Aug 21 '09 at 17:55













Due to lack of time, failed to give more details. Sorry.

– Ikke
Aug 27 '09 at 17:31





Due to lack of time, failed to give more details. Sorry.

– Ikke
Aug 27 '09 at 17:31












6 Answers
6






active

oldest

votes


















8





+50









The code you posted should work as-is. Something else in your code, code that you didn't post here, is causing this to break. It could be clearing the Tag, it could be a data binding set on the tag, etc.



Without seeing all your code, the best I can do is guess and help you isolate the problem.



Here's what I'd do: setup Visual Studio to allow stepping into the .NET framework source code with the debugger. Then, set a breakpoint on the setter for the TreeNode.Tag property. After you set the tag in your code to your AttributePair List, see when it gets set again. The breakpoint will hit, you'll look at the stack trace and see what exactly is clearing your Tag property.






share|improve this answer



















  • 2





    This seems like a very viable answer...

    – JustLoren
    Aug 25 '09 at 21:09



















3














If using Tag property isn't in principle, I'm recommend inherit TreeItem:



public class MyTreeNode : TreeNode
{
public List<AttributePair> list;

public MyTreeNode (string text,List<AttributePair> list) : base(text)
{
this.list = list;
}
//or
public MyTreeNode (string text) : base(text)
{
this.list = new List<AttributePair>();
}
}


And use it:



private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvXML.SelectedNode is MyTreeNode)
{
MyTreeNode selectedNode = tvXML.SelectedNode as MyTreeNode;
selectedNode.list.Add(.., ..);
}
if (e.Node is MyTreeNode)
{
MyTreeNode node = e.Node as MyTreeNode;
node.list.Add(.., ..);
}
}





share|improve this answer































    0














    Maybe you are assigning the values after Select event. Otherwise you can maintain a dictionary of TreeNode and tag values as workaround.






    share|improve this answer
























    • the class that assigns the value, builds the treenodes, so it is not possible that it assigns it after the select event. I thought of that workaround too, but i would like to avoid it

      – Ikke
      Aug 18 '09 at 13:15



















    0














    Try declaring/initialising your List object somewhere above (outside of the inner scope you are in) and when you assign to the .tag property - don't create a new list but rather assign previously created List object.






    share|improve this answer































      0














      Without seeing the rest of your code, we have to make educated guesses. Given what you said in the comments to your question




      In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.




      I would say the control generating the event (the one received through the parameter sender) is not tvXML.



      Do you have other TreeViews on this form?
      Any chance you accidentally bound the event to the wrong instance of TreeView?



      An easy way to check this is comparing sender with tvXML.




      private void tvXML_AfterSelect(object sender, TreeViewEventArgs e) {
      // FOR DEBUG: Check it is the right instance
      if (sender != tvXML) {
      throw new InvalidOperationException();
      }

      if (tvXML.SelectedNode.Tag != null) {
      }
      if (e.Node.Tag != null) {
      }
      }






      share|improve this answer
























      • I only have one Treeview object in my project. I tried to use sender in stead of tvXML, but that makes no difference.

        – Ikke
        Aug 27 '09 at 17:33



















      0














          private TreeViewItem _subsender;
      private object _senderTag;
      public TreeViewItem _sender
      {
      get {
      return _subsender;
      }
      set
      {
      _senderTag = value.Tag;
      _subsender = value;
      }
      }


      Got the same problem this the solution that i found



      Just don't use the .tag but _senderTag



      (don't change the lines in the set for some reason :D )



      (You cant just reset the tag (maybe new TreeViewItem ))






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1292694%2fvalue-of-tag-property-disappears%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8





        +50









        The code you posted should work as-is. Something else in your code, code that you didn't post here, is causing this to break. It could be clearing the Tag, it could be a data binding set on the tag, etc.



        Without seeing all your code, the best I can do is guess and help you isolate the problem.



        Here's what I'd do: setup Visual Studio to allow stepping into the .NET framework source code with the debugger. Then, set a breakpoint on the setter for the TreeNode.Tag property. After you set the tag in your code to your AttributePair List, see when it gets set again. The breakpoint will hit, you'll look at the stack trace and see what exactly is clearing your Tag property.






        share|improve this answer



















        • 2





          This seems like a very viable answer...

          – JustLoren
          Aug 25 '09 at 21:09
















        8





        +50









        The code you posted should work as-is. Something else in your code, code that you didn't post here, is causing this to break. It could be clearing the Tag, it could be a data binding set on the tag, etc.



        Without seeing all your code, the best I can do is guess and help you isolate the problem.



        Here's what I'd do: setup Visual Studio to allow stepping into the .NET framework source code with the debugger. Then, set a breakpoint on the setter for the TreeNode.Tag property. After you set the tag in your code to your AttributePair List, see when it gets set again. The breakpoint will hit, you'll look at the stack trace and see what exactly is clearing your Tag property.






        share|improve this answer



















        • 2





          This seems like a very viable answer...

          – JustLoren
          Aug 25 '09 at 21:09














        8





        +50







        8





        +50



        8




        +50





        The code you posted should work as-is. Something else in your code, code that you didn't post here, is causing this to break. It could be clearing the Tag, it could be a data binding set on the tag, etc.



        Without seeing all your code, the best I can do is guess and help you isolate the problem.



        Here's what I'd do: setup Visual Studio to allow stepping into the .NET framework source code with the debugger. Then, set a breakpoint on the setter for the TreeNode.Tag property. After you set the tag in your code to your AttributePair List, see when it gets set again. The breakpoint will hit, you'll look at the stack trace and see what exactly is clearing your Tag property.






        share|improve this answer













        The code you posted should work as-is. Something else in your code, code that you didn't post here, is causing this to break. It could be clearing the Tag, it could be a data binding set on the tag, etc.



        Without seeing all your code, the best I can do is guess and help you isolate the problem.



        Here's what I'd do: setup Visual Studio to allow stepping into the .NET framework source code with the debugger. Then, set a breakpoint on the setter for the TreeNode.Tag property. After you set the tag in your code to your AttributePair List, see when it gets set again. The breakpoint will hit, you'll look at the stack trace and see what exactly is clearing your Tag property.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 20 '09 at 15:06









        Judah Gabriel HimangoJudah Gabriel Himango

        42.3k32144198




        42.3k32144198








        • 2





          This seems like a very viable answer...

          – JustLoren
          Aug 25 '09 at 21:09














        • 2





          This seems like a very viable answer...

          – JustLoren
          Aug 25 '09 at 21:09








        2




        2





        This seems like a very viable answer...

        – JustLoren
        Aug 25 '09 at 21:09





        This seems like a very viable answer...

        – JustLoren
        Aug 25 '09 at 21:09













        3














        If using Tag property isn't in principle, I'm recommend inherit TreeItem:



        public class MyTreeNode : TreeNode
        {
        public List<AttributePair> list;

        public MyTreeNode (string text,List<AttributePair> list) : base(text)
        {
        this.list = list;
        }
        //or
        public MyTreeNode (string text) : base(text)
        {
        this.list = new List<AttributePair>();
        }
        }


        And use it:



        private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
        {
        if (tvXML.SelectedNode is MyTreeNode)
        {
        MyTreeNode selectedNode = tvXML.SelectedNode as MyTreeNode;
        selectedNode.list.Add(.., ..);
        }
        if (e.Node is MyTreeNode)
        {
        MyTreeNode node = e.Node as MyTreeNode;
        node.list.Add(.., ..);
        }
        }





        share|improve this answer




























          3














          If using Tag property isn't in principle, I'm recommend inherit TreeItem:



          public class MyTreeNode : TreeNode
          {
          public List<AttributePair> list;

          public MyTreeNode (string text,List<AttributePair> list) : base(text)
          {
          this.list = list;
          }
          //or
          public MyTreeNode (string text) : base(text)
          {
          this.list = new List<AttributePair>();
          }
          }


          And use it:



          private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
          {
          if (tvXML.SelectedNode is MyTreeNode)
          {
          MyTreeNode selectedNode = tvXML.SelectedNode as MyTreeNode;
          selectedNode.list.Add(.., ..);
          }
          if (e.Node is MyTreeNode)
          {
          MyTreeNode node = e.Node as MyTreeNode;
          node.list.Add(.., ..);
          }
          }





          share|improve this answer


























            3












            3








            3







            If using Tag property isn't in principle, I'm recommend inherit TreeItem:



            public class MyTreeNode : TreeNode
            {
            public List<AttributePair> list;

            public MyTreeNode (string text,List<AttributePair> list) : base(text)
            {
            this.list = list;
            }
            //or
            public MyTreeNode (string text) : base(text)
            {
            this.list = new List<AttributePair>();
            }
            }


            And use it:



            private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
            {
            if (tvXML.SelectedNode is MyTreeNode)
            {
            MyTreeNode selectedNode = tvXML.SelectedNode as MyTreeNode;
            selectedNode.list.Add(.., ..);
            }
            if (e.Node is MyTreeNode)
            {
            MyTreeNode node = e.Node as MyTreeNode;
            node.list.Add(.., ..);
            }
            }





            share|improve this answer













            If using Tag property isn't in principle, I'm recommend inherit TreeItem:



            public class MyTreeNode : TreeNode
            {
            public List<AttributePair> list;

            public MyTreeNode (string text,List<AttributePair> list) : base(text)
            {
            this.list = list;
            }
            //or
            public MyTreeNode (string text) : base(text)
            {
            this.list = new List<AttributePair>();
            }
            }


            And use it:



            private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
            {
            if (tvXML.SelectedNode is MyTreeNode)
            {
            MyTreeNode selectedNode = tvXML.SelectedNode as MyTreeNode;
            selectedNode.list.Add(.., ..);
            }
            if (e.Node is MyTreeNode)
            {
            MyTreeNode node = e.Node as MyTreeNode;
            node.list.Add(.., ..);
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 20 '09 at 12:26









            ChernikovChernikov

            6221919




            6221919























                0














                Maybe you are assigning the values after Select event. Otherwise you can maintain a dictionary of TreeNode and tag values as workaround.






                share|improve this answer
























                • the class that assigns the value, builds the treenodes, so it is not possible that it assigns it after the select event. I thought of that workaround too, but i would like to avoid it

                  – Ikke
                  Aug 18 '09 at 13:15
















                0














                Maybe you are assigning the values after Select event. Otherwise you can maintain a dictionary of TreeNode and tag values as workaround.






                share|improve this answer
























                • the class that assigns the value, builds the treenodes, so it is not possible that it assigns it after the select event. I thought of that workaround too, but i would like to avoid it

                  – Ikke
                  Aug 18 '09 at 13:15














                0












                0








                0







                Maybe you are assigning the values after Select event. Otherwise you can maintain a dictionary of TreeNode and tag values as workaround.






                share|improve this answer













                Maybe you are assigning the values after Select event. Otherwise you can maintain a dictionary of TreeNode and tag values as workaround.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 18 '09 at 10:29









                Ashok RalhanAshok Ralhan

                212




                212













                • the class that assigns the value, builds the treenodes, so it is not possible that it assigns it after the select event. I thought of that workaround too, but i would like to avoid it

                  – Ikke
                  Aug 18 '09 at 13:15



















                • the class that assigns the value, builds the treenodes, so it is not possible that it assigns it after the select event. I thought of that workaround too, but i would like to avoid it

                  – Ikke
                  Aug 18 '09 at 13:15

















                the class that assigns the value, builds the treenodes, so it is not possible that it assigns it after the select event. I thought of that workaround too, but i would like to avoid it

                – Ikke
                Aug 18 '09 at 13:15





                the class that assigns the value, builds the treenodes, so it is not possible that it assigns it after the select event. I thought of that workaround too, but i would like to avoid it

                – Ikke
                Aug 18 '09 at 13:15











                0














                Try declaring/initialising your List object somewhere above (outside of the inner scope you are in) and when you assign to the .tag property - don't create a new list but rather assign previously created List object.






                share|improve this answer




























                  0














                  Try declaring/initialising your List object somewhere above (outside of the inner scope you are in) and when you assign to the .tag property - don't create a new list but rather assign previously created List object.






                  share|improve this answer


























                    0












                    0








                    0







                    Try declaring/initialising your List object somewhere above (outside of the inner scope you are in) and when you assign to the .tag property - don't create a new list but rather assign previously created List object.






                    share|improve this answer













                    Try declaring/initialising your List object somewhere above (outside of the inner scope you are in) and when you assign to the .tag property - don't create a new list but rather assign previously created List object.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 21 '09 at 2:34









                    DmitryKDmitryK

                    4,83111330




                    4,83111330























                        0














                        Without seeing the rest of your code, we have to make educated guesses. Given what you said in the comments to your question




                        In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.




                        I would say the control generating the event (the one received through the parameter sender) is not tvXML.



                        Do you have other TreeViews on this form?
                        Any chance you accidentally bound the event to the wrong instance of TreeView?



                        An easy way to check this is comparing sender with tvXML.




                        private void tvXML_AfterSelect(object sender, TreeViewEventArgs e) {
                        // FOR DEBUG: Check it is the right instance
                        if (sender != tvXML) {
                        throw new InvalidOperationException();
                        }

                        if (tvXML.SelectedNode.Tag != null) {
                        }
                        if (e.Node.Tag != null) {
                        }
                        }






                        share|improve this answer
























                        • I only have one Treeview object in my project. I tried to use sender in stead of tvXML, but that makes no difference.

                          – Ikke
                          Aug 27 '09 at 17:33
















                        0














                        Without seeing the rest of your code, we have to make educated guesses. Given what you said in the comments to your question




                        In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.




                        I would say the control generating the event (the one received through the parameter sender) is not tvXML.



                        Do you have other TreeViews on this form?
                        Any chance you accidentally bound the event to the wrong instance of TreeView?



                        An easy way to check this is comparing sender with tvXML.




                        private void tvXML_AfterSelect(object sender, TreeViewEventArgs e) {
                        // FOR DEBUG: Check it is the right instance
                        if (sender != tvXML) {
                        throw new InvalidOperationException();
                        }

                        if (tvXML.SelectedNode.Tag != null) {
                        }
                        if (e.Node.Tag != null) {
                        }
                        }






                        share|improve this answer
























                        • I only have one Treeview object in my project. I tried to use sender in stead of tvXML, but that makes no difference.

                          – Ikke
                          Aug 27 '09 at 17:33














                        0












                        0








                        0







                        Without seeing the rest of your code, we have to make educated guesses. Given what you said in the comments to your question




                        In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.




                        I would say the control generating the event (the one received through the parameter sender) is not tvXML.



                        Do you have other TreeViews on this form?
                        Any chance you accidentally bound the event to the wrong instance of TreeView?



                        An easy way to check this is comparing sender with tvXML.




                        private void tvXML_AfterSelect(object sender, TreeViewEventArgs e) {
                        // FOR DEBUG: Check it is the right instance
                        if (sender != tvXML) {
                        throw new InvalidOperationException();
                        }

                        if (tvXML.SelectedNode.Tag != null) {
                        }
                        if (e.Node.Tag != null) {
                        }
                        }






                        share|improve this answer













                        Without seeing the rest of your code, we have to make educated guesses. Given what you said in the comments to your question




                        In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.




                        I would say the control generating the event (the one received through the parameter sender) is not tvXML.



                        Do you have other TreeViews on this form?
                        Any chance you accidentally bound the event to the wrong instance of TreeView?



                        An easy way to check this is comparing sender with tvXML.




                        private void tvXML_AfterSelect(object sender, TreeViewEventArgs e) {
                        // FOR DEBUG: Check it is the right instance
                        if (sender != tvXML) {
                        throw new InvalidOperationException();
                        }

                        if (tvXML.SelectedNode.Tag != null) {
                        }
                        if (e.Node.Tag != null) {
                        }
                        }







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Aug 25 '09 at 21:47









                        Alfred MyersAlfred Myers

                        5,52912761




                        5,52912761













                        • I only have one Treeview object in my project. I tried to use sender in stead of tvXML, but that makes no difference.

                          – Ikke
                          Aug 27 '09 at 17:33



















                        • I only have one Treeview object in my project. I tried to use sender in stead of tvXML, but that makes no difference.

                          – Ikke
                          Aug 27 '09 at 17:33

















                        I only have one Treeview object in my project. I tried to use sender in stead of tvXML, but that makes no difference.

                        – Ikke
                        Aug 27 '09 at 17:33





                        I only have one Treeview object in my project. I tried to use sender in stead of tvXML, but that makes no difference.

                        – Ikke
                        Aug 27 '09 at 17:33











                        0














                            private TreeViewItem _subsender;
                        private object _senderTag;
                        public TreeViewItem _sender
                        {
                        get {
                        return _subsender;
                        }
                        set
                        {
                        _senderTag = value.Tag;
                        _subsender = value;
                        }
                        }


                        Got the same problem this the solution that i found



                        Just don't use the .tag but _senderTag



                        (don't change the lines in the set for some reason :D )



                        (You cant just reset the tag (maybe new TreeViewItem ))






                        share|improve this answer




























                          0














                              private TreeViewItem _subsender;
                          private object _senderTag;
                          public TreeViewItem _sender
                          {
                          get {
                          return _subsender;
                          }
                          set
                          {
                          _senderTag = value.Tag;
                          _subsender = value;
                          }
                          }


                          Got the same problem this the solution that i found



                          Just don't use the .tag but _senderTag



                          (don't change the lines in the set for some reason :D )



                          (You cant just reset the tag (maybe new TreeViewItem ))






                          share|improve this answer


























                            0












                            0








                            0







                                private TreeViewItem _subsender;
                            private object _senderTag;
                            public TreeViewItem _sender
                            {
                            get {
                            return _subsender;
                            }
                            set
                            {
                            _senderTag = value.Tag;
                            _subsender = value;
                            }
                            }


                            Got the same problem this the solution that i found



                            Just don't use the .tag but _senderTag



                            (don't change the lines in the set for some reason :D )



                            (You cant just reset the tag (maybe new TreeViewItem ))






                            share|improve this answer













                                private TreeViewItem _subsender;
                            private object _senderTag;
                            public TreeViewItem _sender
                            {
                            get {
                            return _subsender;
                            }
                            set
                            {
                            _senderTag = value.Tag;
                            _subsender = value;
                            }
                            }


                            Got the same problem this the solution that i found



                            Just don't use the .tag but _senderTag



                            (don't change the lines in the set for some reason :D )



                            (You cant just reset the tag (maybe new TreeViewItem ))







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 11 '17 at 16:47









                            Jan StaesJan Staes

                            11




                            11






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1292694%2fvalue-of-tag-property-disappears%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Florida Star v. B. J. F.

                                Danny Elfman

                                Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues