2013-03-03 255 views
-1

動態創建具有相同項目的組合框我有一個按鈕和一個組合框。然後我點擊這個按鈕動態創建組合框。通過點擊按鈕

這是代碼:

this.Controls.Add(new ComboBox() { Location = new Point(w, z), Width = 121, Height = 21}); 

我該怎麼辦,通過點擊按鈕將創建一個相同的項目組合框?

使用一個組合框我知道怎麼做,但我怎樣才能使用動態創建的組合框,通過單擊按鈕將創建具有相同7個元素的動態組合框?

回答

0

根據我對你的問題的理解(對不起,如果我錯了),你可能想試試這個:

  1. 申報這樣

    private string[] elements = { "A", "B", "C", "D", "E", "F", "G" }; // Sample 7 item to put inside of your dropdownlist 
        private int click = 0; // Initial value of click, will increase 1 after each click. 
    
  2. 2全局變量,然後在button1_click事件,把這個代碼。

    int w = 100, z = 100; // Initial position 
        this.click++; // Click value increase everytime you click 
    
        ComboBox c; 
    
        this.Controls.Add(c = new ComboBox() // Create new combobox 
        { 
         Location = new Point(w, z + (this.click * 30)), // Each time you click, position on x-axis will stay and y-axis will increase by `click` multiply by 30 (you can change this '30' value) 
         Width = 121, 
         Height = 21, 
    
        }); 
    
        for (int i = 0; i < elements.Length; i++) // Loop 7 times 
        { 
         c.Items.Add(this.elements[i]); 
        } 
    
  3. 我希望這回答你的問題:)