TreeView duplicating root nodes, despite there being a single root in Nodes arrayMysterious, ghost-like, impossible-to-find TreeNode textWhy would a TreeView collapse unexpectedly in WinForms?Preventing multiple repeat selection of synchronized Controls?TreeView Where Root And All Nodes Thereafter Are Derived From First Sub-Folder - How To Specify Correct Path?Which form of threading should I use to improve efficiency with recursion?How to populate a Tree View with class variables (Windows Forms)In WPF, Apply different DataTemplate to the same Item at a different level of TreeViewPopulating a TreeView from a list, without root nodes whose children will not have children.Net TreeView EndUpdate is very slowHow to call Focus on a TreeView from the ViewModel

How risky is real estate?

Where can I find a database of galactic spectra?

What is the origin of Scooby-Doo's name?

What is the legal status of travelling with methadone in your carry-on?

How does Powershell create fake drive labels in Windows?

A STL-like vector implementation in C++

Is this one of the engines from the 9/11 aircraft?

How was Hillel permitted to go to the skylight to hear the shiur

Did Karl Marx ever use any example that involved cotton and dollars to illustrate the way capital and surplus value were generated?

Should developer taking test phones home or put in office?

How to remove this component from PCB

Why doesn't a marching band have strings?

Long term BTC investing

I am completely new to Tales from the Floating Vagabond, how do I get started?

Fedora boot screen shows both Fedora logo and Lenovo logo. Why and How?

Apply brace expansion in "reverse order"

What was the Shuttle Carrier Aircraft escape tunnel?

The Target Principal Name Is Incorrect. Cannot Generate SSPI Context (SQL or AD Issue)?

Find the C-factor of a vote

Cascading Repair Costs following Blown Head Gasket on a 2004 Subaru Outback

Can Ogre clerics use Purify Food and Drink on humanoid characters?

Sci fi short story, robot city that nags people about health

Does this Wild Magic result affect the sorcerer or just other creatures?

Can ADFS connect to other SSO services?



TreeView duplicating root nodes, despite there being a single root in Nodes array


Mysterious, ghost-like, impossible-to-find TreeNode textWhy would a TreeView collapse unexpectedly in WinForms?Preventing multiple repeat selection of synchronized Controls?TreeView Where Root And All Nodes Thereafter Are Derived From First Sub-Folder - How To Specify Correct Path?Which form of threading should I use to improve efficiency with recursion?How to populate a Tree View with class variables (Windows Forms)In WPF, Apply different DataTemplate to the same Item at a different level of TreeViewPopulating a TreeView from a list, without root nodes whose children will not have children.Net TreeView EndUpdate is very slowHow to call Focus on a TreeView from the ViewModel






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm currently using TreeView to display a file tree for visualising diffs for a source control project. I have a "Diff" method that recursively edits an existing node in the root "Nodes" array in the TreeView, and then updates the tree afterward.



However, I've encountered an issue where the root node will duplicate for seemingly no reason, despite the debugger telling me there's a single element in the "Nodes" array at the very root of the TreeView, with no indication of an error.



I've already attempted to use "Nodes.Clear()" and then re-add the offending node, however even when clearing the array, the duplicate persists, (even when Nodes.Count is 0). I've also tried using BeginUpdate() and EndUpdate(), but to no avail.



Here's an MCVE:



public partial class BrokenControl : TreeView

public BrokenControl()

InitializeComponent();


public void Go(object sender, EventArgs e)

Nodes.Add("Root");
Nodes[0] = RecursiveEdit(Nodes[0]);
Update();


//This function simply recursively edits the Nodes array.
int iterations = 10;
private TreeNode RecursiveEdit(TreeNode node)

node.Nodes.Add(iterations.ToString());
iterations--;
if (iterations<=0)

return node;


RecursiveEdit(node.Nodes[0]);
return node;




As mentioned, I only expect there to be a single node on the TreeView when it's updated, but instead I get a duplicate node containing duplicated contents of the first.










share|improve this question



















  • 1





    This is a debugging problem Navigate through code with the Visual Studio debugger

    – TheGeneral
    Mar 25 at 9:46












  • This is a UserControl with type TreeView, "Nodes" is a default property of the class, not my own variable. Apologies for the lack of explanation. docs.microsoft.com/en-us/dotnet/api/…

    – Larry Tang
    Mar 25 at 10:00












  • Put a breakpoint on lines containing the text Nodes.Add (all of them). Run the code. How many times (in total) do those breakpoints get hit?

    – mjwills
    Mar 25 at 10:09

















0















I'm currently using TreeView to display a file tree for visualising diffs for a source control project. I have a "Diff" method that recursively edits an existing node in the root "Nodes" array in the TreeView, and then updates the tree afterward.



However, I've encountered an issue where the root node will duplicate for seemingly no reason, despite the debugger telling me there's a single element in the "Nodes" array at the very root of the TreeView, with no indication of an error.



I've already attempted to use "Nodes.Clear()" and then re-add the offending node, however even when clearing the array, the duplicate persists, (even when Nodes.Count is 0). I've also tried using BeginUpdate() and EndUpdate(), but to no avail.



Here's an MCVE:



public partial class BrokenControl : TreeView

public BrokenControl()

InitializeComponent();


public void Go(object sender, EventArgs e)

Nodes.Add("Root");
Nodes[0] = RecursiveEdit(Nodes[0]);
Update();


//This function simply recursively edits the Nodes array.
int iterations = 10;
private TreeNode RecursiveEdit(TreeNode node)

node.Nodes.Add(iterations.ToString());
iterations--;
if (iterations<=0)

return node;


RecursiveEdit(node.Nodes[0]);
return node;




As mentioned, I only expect there to be a single node on the TreeView when it's updated, but instead I get a duplicate node containing duplicated contents of the first.










share|improve this question



















  • 1





    This is a debugging problem Navigate through code with the Visual Studio debugger

    – TheGeneral
    Mar 25 at 9:46












  • This is a UserControl with type TreeView, "Nodes" is a default property of the class, not my own variable. Apologies for the lack of explanation. docs.microsoft.com/en-us/dotnet/api/…

    – Larry Tang
    Mar 25 at 10:00












  • Put a breakpoint on lines containing the text Nodes.Add (all of them). Run the code. How many times (in total) do those breakpoints get hit?

    – mjwills
    Mar 25 at 10:09













0












0








0








I'm currently using TreeView to display a file tree for visualising diffs for a source control project. I have a "Diff" method that recursively edits an existing node in the root "Nodes" array in the TreeView, and then updates the tree afterward.



However, I've encountered an issue where the root node will duplicate for seemingly no reason, despite the debugger telling me there's a single element in the "Nodes" array at the very root of the TreeView, with no indication of an error.



I've already attempted to use "Nodes.Clear()" and then re-add the offending node, however even when clearing the array, the duplicate persists, (even when Nodes.Count is 0). I've also tried using BeginUpdate() and EndUpdate(), but to no avail.



Here's an MCVE:



public partial class BrokenControl : TreeView

public BrokenControl()

InitializeComponent();


public void Go(object sender, EventArgs e)

Nodes.Add("Root");
Nodes[0] = RecursiveEdit(Nodes[0]);
Update();


//This function simply recursively edits the Nodes array.
int iterations = 10;
private TreeNode RecursiveEdit(TreeNode node)

node.Nodes.Add(iterations.ToString());
iterations--;
if (iterations<=0)

return node;


RecursiveEdit(node.Nodes[0]);
return node;




As mentioned, I only expect there to be a single node on the TreeView when it's updated, but instead I get a duplicate node containing duplicated contents of the first.










share|improve this question
















I'm currently using TreeView to display a file tree for visualising diffs for a source control project. I have a "Diff" method that recursively edits an existing node in the root "Nodes" array in the TreeView, and then updates the tree afterward.



However, I've encountered an issue where the root node will duplicate for seemingly no reason, despite the debugger telling me there's a single element in the "Nodes" array at the very root of the TreeView, with no indication of an error.



I've already attempted to use "Nodes.Clear()" and then re-add the offending node, however even when clearing the array, the duplicate persists, (even when Nodes.Count is 0). I've also tried using BeginUpdate() and EndUpdate(), but to no avail.



Here's an MCVE:



public partial class BrokenControl : TreeView

public BrokenControl()

InitializeComponent();


public void Go(object sender, EventArgs e)

Nodes.Add("Root");
Nodes[0] = RecursiveEdit(Nodes[0]);
Update();


//This function simply recursively edits the Nodes array.
int iterations = 10;
private TreeNode RecursiveEdit(TreeNode node)

node.Nodes.Add(iterations.ToString());
iterations--;
if (iterations<=0)

return node;


RecursiveEdit(node.Nodes[0]);
return node;




As mentioned, I only expect there to be a single node on the TreeView when it's updated, but instead I get a duplicate node containing duplicated contents of the first.







c# .net winforms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 9:59







Larry Tang

















asked Mar 25 at 9:42









Larry TangLarry Tang

13412 bronze badges




13412 bronze badges







  • 1





    This is a debugging problem Navigate through code with the Visual Studio debugger

    – TheGeneral
    Mar 25 at 9:46












  • This is a UserControl with type TreeView, "Nodes" is a default property of the class, not my own variable. Apologies for the lack of explanation. docs.microsoft.com/en-us/dotnet/api/…

    – Larry Tang
    Mar 25 at 10:00












  • Put a breakpoint on lines containing the text Nodes.Add (all of them). Run the code. How many times (in total) do those breakpoints get hit?

    – mjwills
    Mar 25 at 10:09












  • 1





    This is a debugging problem Navigate through code with the Visual Studio debugger

    – TheGeneral
    Mar 25 at 9:46












  • This is a UserControl with type TreeView, "Nodes" is a default property of the class, not my own variable. Apologies for the lack of explanation. docs.microsoft.com/en-us/dotnet/api/…

    – Larry Tang
    Mar 25 at 10:00












  • Put a breakpoint on lines containing the text Nodes.Add (all of them). Run the code. How many times (in total) do those breakpoints get hit?

    – mjwills
    Mar 25 at 10:09







1




1





This is a debugging problem Navigate through code with the Visual Studio debugger

– TheGeneral
Mar 25 at 9:46






This is a debugging problem Navigate through code with the Visual Studio debugger

– TheGeneral
Mar 25 at 9:46














This is a UserControl with type TreeView, "Nodes" is a default property of the class, not my own variable. Apologies for the lack of explanation. docs.microsoft.com/en-us/dotnet/api/…

– Larry Tang
Mar 25 at 10:00






This is a UserControl with type TreeView, "Nodes" is a default property of the class, not my own variable. Apologies for the lack of explanation. docs.microsoft.com/en-us/dotnet/api/…

– Larry Tang
Mar 25 at 10:00














Put a breakpoint on lines containing the text Nodes.Add (all of them). Run the code. How many times (in total) do those breakpoints get hit?

– mjwills
Mar 25 at 10:09





Put a breakpoint on lines containing the text Nodes.Add (all of them). Run the code. How many times (in total) do those breakpoints get hit?

– mjwills
Mar 25 at 10:09












1 Answer
1






active

oldest

votes


















1














I managed to fix the issue by using a workaround: instead of directly manipulating the Root node, saving a copy and editing, then clearing and readding, solved my issue.



Still do not know what was causing the dupe even when Nodes.Count was 0 and 1, however this seems to work.



Corrected MCVE:



public partial class BrokenControl : TreeView

...

public void Go(object sender, EventArgs e)

Nodes.Add("Root");
TreeNode savedNode = RecursiveEdit(Nodes[0]);

//This fixes it.
Nodes.Clear();
Nodes.Add(savedNode);

Update();


...






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%2f55334956%2ftreeview-duplicating-root-nodes-despite-there-being-a-single-root-in-nodes-arra%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I managed to fix the issue by using a workaround: instead of directly manipulating the Root node, saving a copy and editing, then clearing and readding, solved my issue.



    Still do not know what was causing the dupe even when Nodes.Count was 0 and 1, however this seems to work.



    Corrected MCVE:



    public partial class BrokenControl : TreeView

    ...

    public void Go(object sender, EventArgs e)

    Nodes.Add("Root");
    TreeNode savedNode = RecursiveEdit(Nodes[0]);

    //This fixes it.
    Nodes.Clear();
    Nodes.Add(savedNode);

    Update();


    ...






    share|improve this answer



























      1














      I managed to fix the issue by using a workaround: instead of directly manipulating the Root node, saving a copy and editing, then clearing and readding, solved my issue.



      Still do not know what was causing the dupe even when Nodes.Count was 0 and 1, however this seems to work.



      Corrected MCVE:



      public partial class BrokenControl : TreeView

      ...

      public void Go(object sender, EventArgs e)

      Nodes.Add("Root");
      TreeNode savedNode = RecursiveEdit(Nodes[0]);

      //This fixes it.
      Nodes.Clear();
      Nodes.Add(savedNode);

      Update();


      ...






      share|improve this answer

























        1












        1








        1







        I managed to fix the issue by using a workaround: instead of directly manipulating the Root node, saving a copy and editing, then clearing and readding, solved my issue.



        Still do not know what was causing the dupe even when Nodes.Count was 0 and 1, however this seems to work.



        Corrected MCVE:



        public partial class BrokenControl : TreeView

        ...

        public void Go(object sender, EventArgs e)

        Nodes.Add("Root");
        TreeNode savedNode = RecursiveEdit(Nodes[0]);

        //This fixes it.
        Nodes.Clear();
        Nodes.Add(savedNode);

        Update();


        ...






        share|improve this answer













        I managed to fix the issue by using a workaround: instead of directly manipulating the Root node, saving a copy and editing, then clearing and readding, solved my issue.



        Still do not know what was causing the dupe even when Nodes.Count was 0 and 1, however this seems to work.



        Corrected MCVE:



        public partial class BrokenControl : TreeView

        ...

        public void Go(object sender, EventArgs e)

        Nodes.Add("Root");
        TreeNode savedNode = RecursiveEdit(Nodes[0]);

        //This fixes it.
        Nodes.Clear();
        Nodes.Add(savedNode);

        Update();


        ...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 10:13









        Larry TangLarry Tang

        13412 bronze badges




        13412 bronze badges





























            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%2f55334956%2ftreeview-duplicating-root-nodes-despite-there-being-a-single-root-in-nodes-arra%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현