【问题标题】:adding multiple elements to a usercontrol? [closed]向用户控件添加多个元素? [关闭]
【发布时间】:2013-05-24 12:40:42
【问题描述】:

我尝试添加更多的布尔值、文本框和组合框等组件。根据代码的底部。但布局很糟糕。我希望它以一种简洁的方式呈现给用户,以便用户快速轻松地更新值。因此,如果我可以指定每种类型应属于的位置,这将有所帮助。顶部是枚举,下面是一堆文本框,等等。

如何做到这一点?动态的,就像在表单设计器中一样。

想象一个用户控件矩形,如果我的道具列表有 enum,enum,bool,bool,text,int,bool。我希望它以友好的方式显示,例如顶部的枚举、中间的文本框、布尔值。等等。

        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();
        }

【问题讨论】:

  • 您的问题到底是什么?你不喜欢你的布局结果如何?您的问题非常含糊不清,我真的不知道您到底在寻找什么。
  • 试图解释,因为我的名声不能发布图片。
  • 所以想象一个用户控件矩形,如果我的道具列表有 enum,enum,bool,bool,text,int,bool。我希望它以友好的方式显示,例如顶部的枚举、中间的文本框、布尔值。等
  • 好的,你还是没有问题。将您的图片发布到 imgur.com 并将其添加为链接。
  • 只需将控件添加到具有垂直方向的 FlowLayoutPanel。

标签: c# .net winforms user-interface


【解决方案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.
}

然后您可以通过PropertyType 属性的整数值对Props 的列表进行排序:

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

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

假设您希望所有bools 出现在ints 之前,您可以简单地更改枚举中的整数值:

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

【讨论】:

  • 谢谢,那么windows窗体或任何东西都没有普通的支持吗?我必须用你的方法?聪明的方法,你以前做过还是刚想出来?
  • 我不是 100% 确定,但我会冒险猜测这是他们不会添加到框架中的东西。您可能能够使用反射来获取按类型组合在一起的字段和属性列表,但我几乎没有反射经验。是的,我只是想出了它,以前从未做过,但没有理由它不应该工作=]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 2012-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多