在我的表单中,我有一个按钮,我想在每次点击时向面板添加一个用户控件:
public partial class AddInstanceForm : MetroForm
{
private List<Material> material { get; set; }
public AddInstanceForm()
{
InitializeComponent();
}
// get data from db for mycombobox which exit in MaterilControl
private void AddInstanceForm_Load(object sender, EventArgs e)
{
using(DBContext db=new DBContext())
{
material = db.Materials.ToList();
}
}
// This Attached to button click
private void anotherMaterial_Click(object sender, EventArgs e)
{
MaterialControl mc = new MaterialControl(material);
this.SuspendLayout();
panel1.Controls.Add(mc);
//EDIT
this.Invalidate();
this.Update();
this.ResumeLayout(false);
}
}
问题是,无论单击按钮多少次,都只会向面板添加一个用户控件:
public partial class MaterialControl : UserControl
{
private MaterialView _material;
private List<Material> MaterialboData { get; set; }
public MaterialView Material
{
get
{
_material.Name=MaterialName.Text;
_material.Quntity = MaterialQu.Text;
_material.MaterialID = Convert.ToInt32(MaterialName.ValueMember);
return _material;
}
set
{
MaterialName.Text=value.Name;
MaterialQu.Text = value.Quntity;
MaterialName.ValueMember = Convert.ToString(_material.MaterialID);
}
}
public MaterialControl(List<Material> Data)
{
_material = new MaterialView();
this.MaterialboData = new List<Material>();
this.MaterialboData = Data;
InitializeComponent();
}
}
答案 0 :(得分:0)
如果您有一个专门用于这些控件的面板,而不是添加新的顶部位置,您可以使用Dock Top,这样可以让面板自动为您设置控件。