0

我有一种情况,我正在构建一个动态表单,单击一个按钮需要生成几个新控件。但是,重要的是每组控件只能看到该组内的控件,而不能看到其他控件。因此,我尝试创建一个单独的类来保存控件,并且希望每次单击都创建该类的一个新实例。

我的问题是如何让它们显示在表单上?

这是包含控件的类:

public class CrmCustomerSearchRow 
{
    public SimpleButton Remove = new SimpleButton();
    public LookUpEdit AttributeList = new LookUpEdit();
    public LookUpEdit ConditionalList = new LookUpEdit();
    public LookUpEdit EnumerableOptions = new LookUpEdit();
    public TextEdit NumericEntry = new TextEdit();
    private readonly StyleController _style = new StyleController();
    private CustomerSearchController _controller = new CustomerSearchController();

    public CrmCustomerSearchRow(int rowX, int rowY, CustomerSearchController control) : this(rowX, rowY, control, false){}

    public CrmCustomerSearchRow(int rowX, int rowY, CustomerSearchController control, bool firstGo)
    {
        //Initializers
        _controller = control;
        Remove = new SimpleButton()
            {
                Enabled = !firstGo,
                Image = Properties.Resources._001_02,
                Location = new System.Drawing.Point(rowX, rowY),
                Name = "simpleButtonRemove",
                Size = new System.Drawing.Size(34, 30),
                StyleController = _style
            };
        AttributeList = new LookUpEdit()
            {
                Location = new System.Drawing.Point(Remove.Location.X + Remove.Width + 4, rowY),
                Name = "lookUpEditAttributeList",
                Size = new System.Drawing.Size(102, 20),
                StyleController = _style
            };
        AttributeList.Properties.Buttons.AddRange(new[]
            {
                new DevExpress.XtraEditors.Controls.EditorButton(
                                                      DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)
            });
        ConditionalList = new LookUpEdit()
            {

                Location = new System.Drawing.Point(AttributeList.Location.X + AttributeList.Width + 4, rowY),
                Name = "lookUpEditConditionalList",
                Size = new System.Drawing.Size(50, 20),
                StyleController = _style
            };
        ConditionalList.Properties.Buttons.AddRange(new[]
            {
                new DevExpress.XtraEditors.Controls.EditorButton(
                                                        DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)
            });
        EnumerableOptions = new LookUpEdit()
            {
                Location = new System.Drawing.Point(ConditionalList.Location.X + ConditionalList.Width + 4, rowY),
                Enabled = false,
                Name = "lookUpEditEnumerableOptions",
                Size = new System.Drawing.Size(91, 20)
            };
        ConditionalList.Properties.Buttons.AddRange(new[]
            {
                new DevExpress.XtraEditors.Controls.EditorButton(
                                                        DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)
            });
        //Datastuffs
        AttributeList.Properties.DataSource = _controller.PropertyTypes;
        AttributeList.Properties.ValueMember = "AttributeTypeGuid";
        AttributeList.Properties.DisplayMember = "AttributeTypeName";
        //Event Handlers
        Remove.Click += Remove_Click;
        AttributeList.EditValueChanged += AttributeList_EditValueChanged;
        //Default Visibility and Enabledness
        ConditionalList.Enabled = false;
        EnumerableOptions.Visible = false;
        NumericEntry.Visible = false;
    }

这是尝试将它们添加到表单中:

        private void simpleButtonAddTemplate_Click(object sender, EventArgs e)
    {
        var oldX = int.Parse(simpleButtonAddTemplate.Location.X.ToString(CultureInfo.InvariantCulture));
        var oldY = int.Parse(simpleButtonAddTemplate.Location.Y.ToString(CultureInfo.InvariantCulture));
        var newX = oldX + int.Parse(simpleButtonAddTemplate.Size.Height.ToString(CultureInfo.InvariantCulture)) + 4;
        var newY = oldY;
        Rows.Add(
            new CrmCustomerSearchRow(oldX, oldY, _controller));
        simpleButtonAddTemplate.Location = new Point(newX, newY);

    }
4

1 回答 1

2

您正在寻找的是一个UserControl. 这是一种定义用户定义控件的方法,该控件由许多其他控件组成,并具有自己的复合功能。让您的自定义类扩展UserControl,而不仅仅是一个独立的类。(您可能还想通过 VS 创建一个 UserControl,因为它会帮助您,而不是让您手动完成所有操作。)

完成后,您可以创建用户控件的实例并将其添加到表单中;作为一个控件本身,这将是完全有效的。

于 2013-10-21T18:11:52.717 回答