将多个元素添加到usercontrol?

时间:2013-05-24 12:40:42

标签: c# .net winforms user-interface

我尝试添加更多bool,textboxes和comboboxes等组件。根据代码的底部。但布局不好。我希望以一种很好的方式呈现这一点,以便用户快速,轻松地更新值。所以如果我可以指定每种类型应该落入哪里,这会有所帮助。顶部的枚举,下面的文本框等等。

如何实现这一目标?动态地,就像在表单设计器中一样。

所以想象一个usercontrol矩形,如果我的道具列表有enum,enum,bool,bool,text,int,bool。我想让它显示在一个友好的管理器上,如顶部的枚举,文本框中间,bools。等。

        private void updateIcons(List<Props> prop) {
        countControls++;
        locationY = 10;
        int gbHeight;
        foreach (var p in prop) {
        radioButtonY = 10;
        IType pType = p.Type;
        if (pType is Enum) {
        var myP = new MyProp(p, this);
        GroupBox gb = new GroupBox();
        gb.Location = new Point(nextLocationX,locationY);
        nextLocationX += rbWidth+10;
        gb.Name = "groupBox" + countControls;
        gb.Text = "smthn";
        var TypesArray = set here;

        gbHeight = TypesArray.Length;
        foreach (var type in TypesArray) {
        getimagesPath(TypesArray);
        RadioButton rb = new RadioButton();
        rb.Appearance = Appearance.Button;
        rb.Width = rbWidth;
        rb.Height = rbHeight;
        rb.Name = type.Name + countControls;
        rb.Text = type.Name;
        string path = imagePaths[type.Name];
        Bitmap rbImage = new Bitmap(path);
        rb.BackgroundImage = rbImage;
        countControls++;
        rb.Location = new Point(radioButtonX, radioButtonY);

        if (myP.Value != null && type.Name.SafeEquals(myP.Value.ToString())) {
        rb.Checked = true;

        }
        radioButtonY += rbHeight;
        gb.Controls.Add(rb);
        rb.CheckedChanged += rb_CheckedChanged;

        }
        gb.Height = rbHeight * gbHeight + 20;
        gb.Width = rbWidth + 10;

        Controls.Add(gb);
        }
        }
        }

        if(pType is string){
         TextBox tb = new TextBox();
          tb.Text = pType.ToString();
        }

1 个答案:

答案 0 :(得分:1)

您可以在类型中添加枚举属性,并按枚举的整数值对列表中的元素进行排序:

class Props
{
    public PropType PropertyType { get; private set; }

    public Props(PropType propType)
    {
        PropertyType = propType;
    }
}

enum PropType
{
    Int32 = 1,
    Int64 = 2,
    Bool = 3 //etc. etc.
}

然后您可以按Props属性的整数值排序PropertyType列表:

prop.OrderBy(p => (int)p.PropertyType);

foreach (var p in prop)
{
    //the rest of your code
}

假设您希望所有bool出现在int之前,您只需更改枚举中的整数值:

enum PropType
{
    Int32 = 2,
    Int64 = 3,
    Bool = 1 //etc. etc.
}