using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public int counter; public ComboBox combB; public string[] combValuesShip; public string[] combValuesCar; public string[] combValuesAirPlane; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { AddNewComboboxLine(); } private void AddNewComboboxLine() { counter++; Label label = new Label(); label.Text = "Label " + counter; ComboBox combA = new ComboBox(); string[] combAValues = new string[] { "Ship", "Car", "AirPlane" }; combA.Items.AddRange(combAValues); combB = new ComboBox(); combA.SelectedIndexChanged += new EventHandler(this.combAChange); label.Name = "label" + counter.ToString(); combA.Name = "combA" + counter.ToString(); combB.Name = "combB" + counter.ToString(); flowLayoutPanel1.Controls.Add(label); flowLayoutPanel1.Controls.Add(combA); flowLayoutPanel1.Controls.Add(combB); combValuesShip = new string[] { "Ferry", "OilTanker", "ContainerVessel" }; combValuesCar = new string[] { "Toyota", "Lada", "Ferrari" }; combValuesAirPlane = new string[] { "Boeing", "Airbus", "Sailplane" }; } private void combAChange(object sender, EventArgs e) { MessageBox.Show(((ComboBox)sender).Name); } private void Form1_Load(object sender, EventArgs e) { } }}
private void combAChange(object sender, EventArgs e) { ComboBox currentCombobox = (ComboBox)sender; string lastChar = currentCombobox.Name.Substring(currentCombobox.Name.Length - 1, 1); if (lastChar == "1") { combB.Items.Clear(); combB.Items.AddRange(combValuesShip); } if (lastChar == "2") { combB.Items.Clear(); combB.Items.AddRange(combValuesCar); } if (lastChar == "3") { combB.Items.Clear(); combB.Items.AddRange(combValuesAirPlane); } }