为什么我不能在另一个类的C#中添加控件?

时间:2018-12-24 21:01:44

标签: c# winforms visual-studio-2013 drop-down-menu controls

我已阅读并观看了有关在编译时添加控件的教程,我试图在我的代码中实现它们,但无法正常工作。我在调试时检查了它,但似乎控件甚至都没有添加到表单中

这是我要实现的类(Parents类具有List的Child类,但是不需要显示它们,因为在调试中我知道它们可以识别。Height和DefaultWidth在DropDownMenu类上是static int)

[Serializable]
public class DropDownMenu
{
    public Button AddButton(string title, int height)
    {
        Button btn = new Button();
        btn.Text = title;
        btn.Size = new Size(DefaultWidth,height);
        return btn;
    }

    public FlowLayoutPanel AddDropDownLayoutPanel(int heightParent, int heightChilds, int numChilds)
    {
        FlowLayoutPanel flpDropDown = new FlowLayoutPanel
        {
            FlowDirection = FlowDirection.TopDown,
            MinimumSize = new Size(DefaultWidth, heightParent),
            MaximumSize = new Size(DefaultWidth, heightChilds*cantChilds)
        };
        return flpDropDown;
    }

    public FlowLayoutPanel AddDropDownButtons(int index, Point iniPos)
    {
        int numChilds = Parents[index].Childs.Count,
        minSizeP = numChilds*HeightChild,
        maxSizeP = minSizeP; //in this code I add more to this variable but right now is not really important

        FlowLayoutPanel flpDropDownP = AddDropDownLayoutPanel(HeightParent, HeightChild, numChilds);
        flpDropDownP.Location = iniPos;
        flpDropDownP.Tag = 1;
        flpDropDownP.Name = "flpParent" + index;

        Button p = AddButton(Parents[index].title, HeightParent);
        p.Name = String.Concat(isBtnParent, index);
        p.Location = new Point(0,0);
        p.Tag = 2;

        flpDropDownP.Controls.Add(p);
        flpDropDownP.MinimumSize = new Size(DefaultWidth, minSizeP);
        flpDropDownP.MaximumSize = new Size(DefaultWidth, maxSizeP);
        flpDropDownP.Size = flpDropDownP.MinimumSize;
        return flpDropDownP;
    }
}

在加载事件中,我将其称为:

private void FrmContainer_Load(object sender, EventArgs e)
    {
    DropDownMenu dropDownMenu = new DropDownMenu();
    List<Parent> parents = new List<Parent>();

    DropDownMenu.HeightParent = this.btnUsuario.Height;
    DropDownMenu.HeightChild = this.btnUsuario.Height - 2;

    try
    {
        List<Child> childs0 = new List<Child>
        {
            new Child{Id = "h0", Nombre = "Child 01"},
            new Child{Id = "h1", Nombre = "Child 02"}
        };
        parents.Add(new Parent{ Id = "p0", Nombre = "Parent 01", Childs = childs0 });
        parents.Add(new Parent{ Id = "p1", Nombre = "Parent 02", Childs= childs0 });

        dropDownMenu.Parents = parents;
        DropDownMenu.DefaultWidth = 128;

        Point iniBtnMenu = new Point(10, 20); //I have other numbers, but isn't really important here

        if (parents.Count != 0)
        {
            for (int index = 0; index < parents.Count; index++)
            {
                if (parents[index].Childs.Count != 0)
                {
                    this.Controls.Add(dropDownMenu.AddDropDownButtons(index, iniBtnMenu));
                }
                else
                {
                    Button n = dropDownMenu.AddButton(parents[index].Nombre, DropDownMenu.HeightParent);
                    n.Location = iniBtnMenu;
                    this.Controls.Add(n);
                }
                iniBtnMenu.X += DropDownMenu.DefaultWidth;
            }
        }
    }
catch (Exception ex)
{
}

}

我认为它们可能是在表单上其他元素的后面创建的,但是在Visual Studio中,当我再次查看添加到表单中的控件时,按钮不存在,所以我想这不是问题。我正在使用Visual Studio 2013(但我认为与它无关)。最初,我只是创建没有大小,位置或名称的按钮,但是当我在Form1.Designer.cs上选中时,所有控件都有它们,因此我添加了它们。


总而言之,我想要的是一个下拉菜单,该菜单取决于对象列表(以后我可以用数据库中的某些数据替换)以及它的子项(例如菜单和子菜单)。而且我没有使用Visual Studio中的默认菜单控件,因为我想修改设计,而我不能使用该控件。

0 个答案:

没有答案