我有一种情况,我正在构建一个动态表单,单击一个按钮需要生成几个新控件。但是,重要的是每组控件只能看到该组内的控件,而不能看到其他控件。因此,我尝试创建一个单独的类来保存控件,并且希望每次单击都创建该类的一个新实例。
我的问题是如何让它们显示在表单上?
这是包含控件的类:
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);
}