Results 1 to 3 of 3

Thread: [RESOLVED] Appending to TreeView via Delegate

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Resolved [RESOLVED] Appending to TreeView via Delegate

    Using code converted from VB.NET, I can successfully invoke a public delegate (from code within Form1 or code in another class) that calls a AppendTreeview method in Form1. However, the 2nd time the method is visited, the node count is zero, so the TreeView is not persistent. I believe from my reading about C#, this is because, unlike VB.NET where an active instance (GUI) of Form1 "owns" each control on it, in C# Form1 does not own each control on it. Hence, control values are not persistent. It's either that, or the fact that that if the GUI is updated or refreshed, control elements can be wiped out. I don't know if TreeView is a special case of this, but I am starting to find articles which state the TreeView is known for not holding its elements consistently when the GUI is updated(?)

    The delegate is public:

    Code:
    public delegate void AppendTreeviewDelegate(byte treeLevel, string rootName, string nodeName, string nodeTxt, string resourceIconName);
    Here's the successful invoke statement when used in an independent class (not Form 1):

    Code:
    Form1 main = (Form1)System.Windows.Forms.Application.OpenForms[0];
    main.Invoke(new AppendTreeviewDelegate(main.AppendTreeview), new object[] {treeLevel, rootName, nodeName, nodeTxt, resourceIconName});

    Here is the successful invoke statement used within Form1 code:

    Code:
    AppendTreeview(treeLevel, rootName, nodeName, nodeTxt, resourceIconName);
    Below is the TreeView append method in Form1:

    Code:
    public void AppendTreeview(byte treeLevel, string rootName, string nodeName, string nodeTxt, string resourceIconName)
    {
      if (treeLevel == 0)
      {
                    TreeView1.BeginUpdate();
                    TreeView1.Nodes.Add(rootName,  nodeTxt, resimgname);  //When triggered 2nd time, Node Count = 0
                    TreeView1.EndUpdate();
      }
      if (treeLevel == 1)
      {
                    TreeView1.Nodes.Find(rootName, true)[0].Nodes.Add(nodeName,  nodeTxt, resourceIconName);
      }
      TreeView1.ExpandAll();
      TreeView1.Refresh();
      return;
    }
    Now, if I rename all of the TreeView1... lines above to Form1.DefaultInstance.TreeView1..., then the Nodes don't drop each time the append method is visited. However, the last time I checked, I could not see anything on the TreeView. So the choices appear to be (a) use TreeView... and nodes will be dropped, or use (b) Form1.DefaultInstance.TreeView1... and nodes will be persistent each time the append method is visited, but there will be nothing visible in the TreeView1 control.
    Last edited by pel11; Feb 8th, 2023 at 09:20 AM.

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

    Re: Invoking Delegate for Method in Form 1

    The issue is probably the arguments but you haven't provided enough information to tell. If you call the method directly in Form1, instead of using the delegate, do you get the same result? If so then that would suggest that the whole delegate thing is a red herring. Show us the stack trace from the exception and show us exactly how you get those argument values and what they are in each case.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Re: Invoking Delegate for Method in Form 1

    Thanks, I got the Invoke to work within Form 1 by simply calling the Appendtreeview method. (All code is now provided above). Outside of Form1, when using the delegate, as long as I invoked with following, the appending worked:

    Code:
    Form1 main = (Form1)System.Windows.Forms.Application.OpenForms[0];
    main.Invoke(new AppendTreeviewDelegate(main.AppendTreeview), new object[] {treeLevel, rootName, nodeName, nodeTxt, resourceIconName});
    Last edited by pel11; Feb 8th, 2023 at 09:19 AM.

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
  •  



Click Here to Expand Forum to Full Width