通过点击按钮

问题描述:

动态创建具有相同项目的组合框我有一个按钮和一个组合框。然后我点击这个按钮动态创建组合框。通过点击按钮

这是代码:

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

我该怎么办,通过点击按钮将创建一个相同的项目组合框?

使用一个组合框我知道怎么做,但我怎样才能使用动态创建的组合框,通过单击按钮将创建具有相同7个元素的动态组合框?

这可能帮助:https://github.com/vitalets/x-editable

X编辑是围绕建立一个页面中的新元素构建的。

根据我对你的问题的理解(对不起,如果我错了),你可能想试试这个:

  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. 我希望这回答你的问题:)