如何根据树视图大小动态调整对象大小

时间:2018-03-02 11:43:13

标签: c# vb.net

我正在尝试通过组合Treeview和NumericUpDown函数来创建一个简单的订单菜单,如下面链接中提供的图像所示

https://imgur.com/a/OB1kv

问题是,每当我展开或折叠子节点时,NumericUpDown对象都不会跟随。我尝试使用以下代码解决问题,但是当感觉子项节点增加时,需要调整每个对象的位置的荒谬数量,这感觉不正确。那么有没有可能的方法将对象的位置锁定到相应的节点?我对treeview命令很新,所以欢迎任何建议!

    NumericUpDown1.Location = New Point(NumericUpDown2.Location.X - 0, NumericUpDown2.Location.Y - 32)

1 个答案:

答案 0 :(得分:1)

我通过制作NumericUpDowns来解决它。我花了一段时间才做到这一点,但是当你展开并在折叠每个节点时删除它们时它会产生数值。

public static List<NumericUpDown> lUpDown = new List<NumericUpDown>();      //List of Updowns we will be using


    private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
    {
        if (e.Node.Level != 0)  //Not adding a NumericUpDown on the Drinks tab
        {
            NumericUpDown newUpDown = new NumericUpDown() { Name = "upDown" + e.Node.Index.ToString() + "_" + e.Node.Parent.Index.ToString() };    //Making a NumericUpDown with a unique name linked to the index of the subnode(Apple juice = 0, Orange Juice = 1, ...) + "_" + index of the node(Drinks, ...) so you can add more subcategories, like food
            Controls.Add(newUpDown);    //Adding to the controls
            newUpDown.BringToFront();   //Bringing to front of the treeview
            lUpDown.Add(newUpDown);     //Adding to our list
        }
        UpdateLocations(e.Node.TreeView);   //Updating location of all NumericUpDowns
    }

    private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
    {
        if (e.Node.Level == 0)
        {
            foreach (TreeNode subNode in e.Node.Nodes)          //Closing all subnodes which automatically will erase their NumericUpDowns
            {
                subNode.Collapse();
            }
        }
        else
        {
            NumericUpDown UpDownToRemove = lUpDown.Find(x => x.Name == "upDown" + e.Node.Index.ToString() + "_" + e.Node.Parent.Index.ToString());     //Finding by the index which NumericUpDown we'll remove
            Controls.Remove(UpDownToRemove);    //Removing from Controls
            lUpDown.Remove(UpDownToRemove);     //Removing from our list

            UpdateLocations(e.Node.TreeView);   //Updating location of all NumericUpDowns
        }
    }

    private void UpdateLocations(TreeView tv)
    {
        int index = 0;
        foreach (TreeNode Node in tv.Nodes)     //Going through all nodes in the TreeView(in this case, only Drinks)
        {
            foreach (TreeNode subNode in Node.Nodes)        //Going through all subnodes(childs of Drinks: Apple Juice, Orange Juice, ...)
            {
                index = subNode.Index;                      //Finding the index of the child
                if (subNode.IsExpanded)
                {
                    Point upDownLoc = subNode.FirstNode.Bounds.Location;                                //Geting the location of the Price tag
                    upDownLoc.X += 80;                                                                  //Adding some X points so it goes to the right
                    upDownLoc.Y += subNode.Bounds.Height - 5;                                           //Correcting the height
                    lUpDown.Find(x => x.Name == "upDown" + index.ToString() + "_" + subNode.Parent.Index.ToString()).Location = upDownLoc;      //Finding the correct UpDown to update location by the subnode index(Apple juice = 0, Orange Juice = 1, ...)
                }
            }
        }
    }

希望它有助于实现它的更复杂的方式。 这是gif的工作