简体   繁体   English

如何使用从 windows 表格 C# 的“comboboxA”中选择来更改“comboboxB”的项目来源

[英]How to change the item source of "comboboxB"using selection from "comboboxA" of windows form C#

Two Combo-Boxes are added to a Windows form dynamically through code.通过代码将两个组合框动态添加到 Windows 表单中。 Now the based on the selection from the Combo-Box-A i want to change the item source of Combo-Box-B.现在基于从 Combo-Box-A 中的选择,我想更改 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组合框 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.默认情况下,组合框 B 的项目源是 itemsource_1,但是当组合框 A 的选择发生变化时,组合框 b 的项目源应该根据选择而改变。

for example if selection of combo box A changes to 4 the item source of combo box B should change to itemsource_4.例如,如果组合框 A 的选择更改为 4,则组合框 B 的项目源应更改为 itemsource_4。

I have added a event handler but I'm unable to access the combo box b in the event handler.我添加了事件处理程序,但无法访问事件处理程序中的组合框 b。 Any help would be really appreciated..任何帮助将非常感激..

i think:我认为:

private void comboxa_SelectedIndexChanged(object sender, EventArgs e)
        {
             comboxb.Items.Clear();
             comboxb.Items.AddRange(itemsource_1);

        }

 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);
            };

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#WPF如何通过组合框选择更改帧源? - C# WPF how to change frame source through combobox selection? 如何在Windows窗体应用程序(C#)中更改在文本上而不是在值上展开的选定组合框项目? - How to change selected combobox item depanding on text instead of value in windows form application (C#)? 如何从另一个表单(formBorderStyle等)更改表单属性值? C#和Windows表单 - How to change form property value from another form(formBorderStyle, etc)? C# and windows form 如何从列表框控件c#windows窗体中删除当前的SelectedIndex项 - how to remove current SelectedIndex item from Listbox control c# windows form 如何防止将新项目添加到C#Windows窗体Listview? - How to prevent adding a new item into the C# Windows Form Listview? 如何在运行时通过 windows 表单 c# 中的按钮更改表单大小 - How to change form size at runtime by a button in windows form c# 如何以 c# windows 形式将文本从源文本框复制到目标文本框? - How to copy text from source to destination textbox in c# windows form? 如何从C#windows窗体应用程序中的列表项中提取Url元素 - How to extract Url elements from list item in C# windows form app Windows窗体 - 如何在C#中的组合框项目中添加标题无法选择? - Windows Form - How add header not selectable in combobox item in C#? C#如何更改另一个窗体(Windows窗体)上的int值 - C# How to change a int value on another form (windows form)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM