2015-05-14 62 views
0

添加用戶控件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(); 
     } 
} 
+0

嘗試在繪製控件後調用Invalidate()。 – Oscar

+0

仍然無法正常工作 –

回答

0

而是用新的頂部位置加入他們,如果你有一個專門只爲這些控制面板,你可以使用Dock頂部,而不是和這會讓面板自動爲您設置控件。

+0

我試過了,但沒有解決問題探針 –

+0

我嘗試了無效+停靠頂部沒有工作 –

+0

將簡歷失效並更新。 – mageos