Reputation: 5
I would like to create a c# windows form where if a button 'Add combobox' is clicked, the system will create 1 instance of 'combobox' every click.
The positions of each instance will be plus 5px longitude below each other.
If possible kindly populate too the new instance with some fix values.
Upvotes: 0
Views: 1021
Reputation: 7105
You can do something like this on button click (comments are in code):
private void button1_Click(object sender, EventArgs e)
{
//create new combo
ComboBox cbo = new ComboBox();
//fill it with values
cbo.Items.Add("value1");
cbo.Items.Add("value2");
//set left location
cbo.Left = 10;
//set default top location
int top = 5;
//if this form contains more then 0 combo boxes, get last combo box Top value and increase it with 5.
if (this.Controls.OfType<ComboBox>().Count() > 0)
top = this.Controls.OfType<ComboBox>().Last().Top + 5;
//set top value to new combo
cbo.Top = top;
//add it to forms control collection
this.Controls.Add(cbo);
}
Upvotes: 2