This is how I create dynamic ComboBoxes by pressing a button:

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

How to make each created combo box be assigned the same elements automatically? Through ComboBox.Items.Add I can not, because it is required for a specific combo box. How to make an automatic addition, assignment?

    3 answers 3

    Specify for each of them the same DataSource containing the elements you need.

    • I need a database in the same place. A simple method like that? - navi1893
    • in the comments to the question itself, the easiest way is indicated. - Sofver

    I do not understand: why not just

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

    ?

      In your case, it is worth looking towards Control.DataBindings and BindingSource.
      It might look something like this:

       using System; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.comboBox1.DataBindings.Add( "Width", this.bindingSource1, "WidthProperty"); this.comboBox1.DataBindings.Add( "Height", this.bindingSource1, "HeightProperty"); this.comboBox2.DataBindings.Add( "Width", this.bindingSource1, "WidthProperty"); this.comboBox2.DataBindings.Add( "Height", this.bindingSource1, "HeightProperty"); this.bindingSource1.DataSource = new Test1[] { new Test1{ HeightProperty = 20, WidthProperty = 200 }};} private void Form1_Load(object sender, EventArgs e) { } } class Test1 { public Int32 Width { get; set; } public Int32 Height { get; set; } } }