我的情况是我正在构建动态表单,单击按钮需要生成几个新控件。但是,重要的是每组控件只能看到该组中的控件,而不是其他控件。所以,我试图创建一个单独的类来保存控件,我希望每次单击都能创建这个类的新实例。
我的问题是如何让它们显示在表单上?
这是包含控件的类:
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);
}
答案 0 :(得分:2)
您要找的是UserControl
。这是一种定义用户定义控件的方法,该控件由许多其他控件组成,并具有自己的复合功能。让您的自定义类扩展UserControl
而不仅仅是一个独立的类。 (你也可能想通过VS创建一个UserControl,因为它可以帮助你,而不是让你手动完成所有事情。)
完成后,您只需创建用户控件的实例并将其添加到表单中;作为一个控制本身,这将是完全有效的。