Two Combo-Boxes are added to a Windows form dynamically through code. Now the based on the selection from the Combo-Box-A i want to change the item source of Combo-Box-B.
Here are some snippets
Adding combo boxes dynamically
string[] comboboxAitemsource = { "1", "2", "3", "4"};
string[] itemsource_1 = { "11", "12", "13", "14"};
string[] itemsource_2 = { "21", "22", "23", "24"};
string[] itemsource_3 = { "31", "32", "33", "34"};
string[] itemsource_4 = { "41", "42", "43", "44"};
int left_margin = 10, top_margin = margin, margin_inc = 40, sec_left_margin = 300;
System.Windows.Controls.Label ComboboxAlabel = new System.Windows.Controls.ComboboxAlabel();
ComboboxAlabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; ;
ComboboxAlabel.VerticalAlignment = VerticalAlignment.Top;
ComboboxAlabel.Name = "typeComboboxAlabel";
ComboboxAlabel.Content = "COMBOBOX A";
ComboboxAlabel.Height = 40;
ComboboxAlabel.Margin = new Thickness((double)left_margin, (double)top_margin, (double)10, (double)0);
grid.Children.Add(ComboboxAlabel);
System.Windows.Controls.ComboBox comboboxA = new System.Windows.Controls.ComboBox();
comboboxA.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; ;
comboboxA.VerticalAlignment = VerticalAlignment.Top;
comboboxA.Name = "typeCombobox";
comboboxA.Height = 20;
comboboxA.Width = 250;
comboboxA.Margin = new Thickness((double)sec_left_margin, (double)top_margin, (double)10, (double)0);
comboboxA.ItemsSource = comboboxAitemsource;
comboboxA.SelectedIndex = 0;
comboboxA.SelectionChanged = new System.Windows.Controls.SelectionChangedEventHandler(typeCombobox_SelectionChangedEventHandler);
top_margin = top_margin margin_inc;
grid.Children.Add(comboboxA);
System.Windows.Controls.Label comboboxBlabel = new System.Windows.Controls.Label();
comboboxBlabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
comboboxBlabel.VerticalAlignment = VerticalAlignment.Top;
comboboxBlabel.Name = "typeLabelUpdate";
comboboxBlabel.Content = "COMBOBOX B";
comboboxBlabel.Height = 40;
comboboxBlabel.Margin = new Thickness((double)left_margin, (double)top_margin, (double)10, (double)0);
grid.Children.Add(comboboxBlabel);
System.Windows.Controls.ComboBox comboboxB = new System.Windows.Controls.ComboBox();
comboboxB.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
comboboxB.VerticalAlignment = VerticalAlignment.Top;
comboboxB.Name = "typeComboBoxUpdate";
comboboxB.Height = 20;
comboboxB.Width = 250;
comboboxB.Margin = new Thickness((double)sec_left_margin, (double)top_margin, (double)10, (double)0);
comboboxB.ItemsSource = itemsource_1;
comboboxB.SelectedIndex = 0;
top_margin = top_margin margin_inc;
grid.Children.Add(comboboxB);
Event handler for combo box A
private void typeCombobox_SelectionChangedEventHandler(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
}
By default item source for combo box B is itemsource_1, but when the selection changes on combo box A the item source of combo box b should change based on the selection.
for example if selection of combo box A changes to 4 the item source of combo box B should change to itemsource_4.
I have added a event handler but I'm unable to access the combo box b in the event handler. Any help would be really appreciated !.
CodePudding user response:
i think :
private void comboxa_SelectedIndexChanged(object sender, EventArgs e)
{
comboxb.Items.Clear();
comboxb.Items.AddRange(itemsource_1);
}
CodePudding user response:
private void d()
{
string[] comboboxAitemsource = { "1", "2", "3", "4" };
string[] itemsource_1 = { "11", "12", "13", "14" };
string[] itemsource_2 = { "21", "22", "23", "24" };
string[] itemsource_3 = { "31", "32", "33", "34" };
string[] itemsource_4 = { "41", "42", "43", "44" };
int left_margin = 10, top_margin = margin, margin_inc = 40, sec_left_margin = 300;
System.Windows.Controls.Label ComboboxAlabel = new System.Windows.Controls.ComboboxAlabel();
ComboboxAlabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; ;
ComboboxAlabel.VerticalAlignment = VerticalAlignment.Top;
ComboboxAlabel.Name = "typeComboboxAlabel";
ComboboxAlabel.Content = "COMBOBOX A";
ComboboxAlabel.Height = 40;
ComboboxAlabel.Margin = new Thickness((double)left_margin, (double)top_margin, (double)10, (double)0);
grid.Children.Add(ComboboxAlabel);
System.Windows.Controls.ComboBox comboboxA = new System.Windows.Controls.ComboBox();
comboboxA.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; ;
comboboxA.VerticalAlignment = VerticalAlignment.Top;
comboboxA.Name = "typeCombobox";
comboboxA.Height = 20;
comboboxA.Width = 250;
comboboxA.Margin = new Thickness((double)sec_left_margin, (double)top_margin, (double)10, (double)0);
comboboxA.ItemsSource = comboboxAitemsource;
comboboxA.SelectedIndex = 0;
comboboxA.SelectionChanged = new System.Windows.Controls.SelectionChangedEventHandler(typeCombobox_SelectionChangedEventHandler);
top_margin = top_margin margin_inc;
grid.Children.Add(comboboxA);
System.Windows.Controls.Label comboboxBlabel = new System.Windows.Controls.Label();
comboboxBlabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
comboboxBlabel.VerticalAlignment = VerticalAlignment.Top;
comboboxBlabel.Name = "typeLabelUpdate";
comboboxBlabel.Content = "COMBOBOX B";
comboboxBlabel.Height = 40;
comboboxBlabel.Margin = new Thickness((double)left_margin, (double)top_margin, (double)10, (double)0);
grid.Children.Add(comboboxBlabel);
System.Windows.Controls.ComboBox comboboxB = new System.Windows.Controls.ComboBox();
comboboxB.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
comboboxB.VerticalAlignment = VerticalAlignment.Top;
comboboxB.Name = "typeComboBoxUpdate";
comboboxB.Height = 20;
comboboxB.Width = 250;
comboboxB.Margin = new Thickness((double)sec_left_margin, (double)top_margin, (double)10, (double)0);
comboboxB.ItemsSource = itemsource_1;
comboboxB.SelectedIndex = 0;
top_margin = top_margin margin_inc;
grid.Children.Add(comboboxB);
comboboxA.SelectedIndexChanged = (s, e) => {
comboboxB.Items.Clear();
comboboxB.Items.AddRange(itemsource_1);
};
}