添加用戶控件dynammiclly到面板在我的表單我有一個按鈕,我希望每個點擊時用戶控件添加到面板:上buttonClick
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();
}
}
嘗試在繪製控件後調用Invalidate()。 – Oscar
仍然無法正常工作 –