0

Currently I can create multiple treeview using a for loop and show those tree view to tabpage but my root node doesnt show. This is what I have so far a

 private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < ThreatEvaluationInstances.Count; i++)
        {
            string a = i.ToString();
            TreeView tv  = new TreeView();
            tv.Name = "tv" + i;
            tv .Location = new Point(8, i* 100);
            tv.Size = new Size(841, 125);
            TreeNode root = new TreeNode(ThreatEvaluationInstances.ElementAt(i).Threat1 + " (" + ThreatEvaluationInstances.ElementAt(i).AttackPotential1 + ") ");

            AttackTrees1111.Controls.Add(tv);
            tv.Nodes.Add(root);


        }
        //populateTreeview();
    }

enter image description here As you can see multiple tree views are on screen but only 1 root node shows and that is from the first loop when i=0, my goal is to show different rood node for each treeview. How can I achieve that?

  • What is the height of your TreeView controls (compare to their `Location.Y`)? Instead of calculating an offset, add these controls to a FlowLayoutPanel, it'll tale care of it. – Jimi Jan 08 '20 at 17:57
  • tv.Size = new Size(841, 125); height is 125. The height and the width is staying same but im changing the location of the treeview in each loop tv .Location = new Point(8, i* 100);. If you click on the image i added you can see i have 4 different treeview but the root node not getting inserted on all treeview – Ali Chowdhury Jan 08 '20 at 18:03
  • Yes, the size is `(841, 125)`. The Location of the first one is then in `(8, 0)`, the second one in `(8, 100)`. Is there something wrong here? – Jimi Jan 08 '20 at 18:05
  • Thank you Jimi i addeded my treeview to flowlayoutpanel and the output is showing just the way I wanted it to be. Thank you so much!! – Ali Chowdhury Jan 08 '20 at 18:09
  • How can i add a menustripitem to all the dynamically created treeviews? – Ali Chowdhury Jan 08 '20 at 18:41
  • Do you mean a [ContextMenuStrip](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.contextmenustrip)? Create a (one) new ContextMenuStrp in the Form Designer, add handlers to the Opened, Closed and Click events, then set the `[TreeView].ContextMenuStrip = yourContextMenuStrip;` before adding it to the FlowLayoutPanel. You can then handle the ContextMenu activation as shown here: [Get the SourceControl of a DropDownMenu](https://stackoverflow.com/a/53263702/7444103) – Jimi Jan 08 '20 at 19:08
  • woow I never thought of that I kept adding my ContextMenuStrip item to flowLauoutPanel as a SourceControl assuming it would work. Thank you again, you have been a great help!! – Ali Chowdhury Jan 08 '20 at 19:15

0 Answers0