C#组合框数据绑定筛选器

C#组合框数据绑定筛选器,c#,dictionary,C#,Dictionary,我有个小问题。我有一组组合框,它们绑定到与一组字典关联的列表,这些字典为数学方程提供int值。我需要根据cb_test_1的选择过滤cb_test_2的结果。我想我很接近了。当我在cb_test_1中更改值时会发生什么情况?它将正确的列表作为数据绑定应用到组合框,但是,它会将其加倍,将列表放置在组合框中两次。我似乎找不到代码中的原因。请看一看。我欢迎任何帮助和批评 namespace Break { partial class MainForm { double x, y, z;

我有个小问题。我有一组组合框,它们绑定到与一组字典关联的列表,这些字典为数学方程提供int值。我需要根据cb_test_1的选择过滤cb_test_2的结果。我想我很接近了。当我在cb_test_1中更改值时会发生什么情况?它将正确的列表作为数据绑定应用到组合框,但是,它会将其加倍,将列表放置在组合框中两次。我似乎找不到代码中的原因。请看一看。我欢迎任何帮助和批评

namespace Break
{
partial class MainForm
{
    double x, y, z;     

    System.Collections.Generic.List<string> cable_size0;
    System.Collections.Generic.List<string> cable_size1;
    //System.Collections.Generic.List<string> cable_size2;
    //System.Collections.Generic.List<string> cable_size3;
    //List<string> cable_size4;
    //List<string> cable_size5;
    //List<string> cable_size6;
    //List<string> cable_size7;
    //List<string> cable_size8;
    //List<string> cable_size9;
    //List<string> cable_size10;        
    System.Collections.Generic.List<string> system_type_list;
    System.Collections.Generic.List<string> amperage;

    System.Collections.Generic.Dictionary<string, double> cable_dictionary_0;
    System.Collections.Generic.Dictionary<string, double> cable_dictionary_1;

    System.Collections.Generic.Dictionary<string, double> system_type;
    System.Collections.Generic.Dictionary<string, double> amperage_dictionary;

private void MainFormLoad(object sender, EventArgs e)
    {
            //Each of the following lists populate the ComboBoxes
            //to be selected on the main form.
            amperage = new List<string>();
            system_type_list = new List<string>();
            //Filter
            cable_size0 = new List<string>();
            cable_size1 = new List<string>();
            //cable_size2 = new List<string>();
            //cable_size3 = new List<string>();
            //cable_size4 = new List<string>();
            //cable_size5 = new List<string>();
            //cable_size6 = new List<string>();
            //cable_size7 = new List<string>();
            //cable_size8 = new List<string>();
            //cable_size9 = new List<string>();
            //cable_size10 = new List<string>();


            system_type = new Dictionary<string, double>();
            amperage_dictionary = new Dictionary<string, double>();
            //Filter
            cable_dictionary_0 = new Dictionary<string, double>();
            cable_dictionary_1 = new Dictionary<string, double>();
            //cable_dictionary_2 = new Dictionary<string, double>();
            //cable_dictionary_3 = new Dictionary<string, double>();
            //cable_dictionary_4 = new Dictionary<string, double>();
            //cable_dictionary_5 = new Dictionary<string, double>();
            //cable_dictionary_6 = new Dictionary<string, double>();
            //cable_dictionary_7 = new Dictionary<string, double>();
            //cable_dictionary_8 = new Dictionary<string, double>();
            //cable_dictionary_9 = new Dictionary<string, double>();
            //cable_dictionary_10 = new Dictionary<string, double>();

            //
            InitializeCurrentLoadCB();
            InitializeSystemTypeCB();
            //Cable size filter
            if ((cb_test_1.SelectedIndex==0)||
                (cb_test_1.SelectedIndex==1)||
                (cb_test_1.SelectedIndex==2)||
                (cb_test_1.SelectedIndex==3))
            {
                InitializeCableSizeCB0();
                InitializeCableLookup0();
            }

            //InitializeCableSizeCB2();
            //InitializeCableSizeCB3();
            //InitializeCableSizeCB4();
            //InitializeCableSizeCB5();
            //InitializeCableSizeCB6();
            //InitializeCableSizeCB7();
            //InitializeCableSizeCB8();
            //InitializeCableSizeCB9();
            //InitializeCableSizeCB10();


            //---------------Dictionaries---------------------------------------------------------
            InitializeSystemTypeLookup();
            InitializeAmperageLookup();
            //Cable Size Filter


            //InitializeCableLookup2();
            //InitializeCableLookup3();
            //InitializeCableLookup4();
            //InitializeCableLookup5();
            //InitializeCableLookup6();
            //InitializeCableLookup7();
            //InitializeCableLookup8();
            //InitializeCableLookup9();
            //InitializeCableLookup10();

    }

    private void Cb_test_1SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((cb_test_1.SelectedIndex==0)||
                (cb_test_1.SelectedIndex==1)||
                (cb_test_1.SelectedIndex==2)||
                (cb_test_1.SelectedIndex==3))
            {
                InitializeCableSizeCB1();                   
            }
    }


    private void InitializeCurrentLoadCB()
    {
            //Amperage List, No Exclusions-----------------------------------------------------------

            amperage.Add("Please Select Amperage");
            amperage.Add("400");
            amperage.Add("800");
            amperage.Add("1000");
            amperage.Add("1200");
            amperage.Add("1600");
            amperage.Add("2000");
            amperage.Add("2500");
            amperage.Add("3000");
            amperage.Add("3200");
            amperage.Add("4000");
            amperage.Add("5000");
            amperage.Add("6000");

            cb_test_1.DataSource = amperage;
            cb_test_1.SelectedIndex = 0;
            cb_test_1.DropDownStyle = ComboBoxStyle.DropDownList;
    }

    private void InitializeCableSizeCB0()
    {           
            //Cable List, No Exclusions --------------------------------------------------------------
            cable_size0.Add("Please Select Cable Size");
            cable_size0.Add ("#1");
            cable_size0.Add ("1/0");
            cable_size0.Add ("2/0");
            cable_size0.Add ("3/0");
            cable_size0.Add ("4/0");
            cable_size0.Add ("250");
            cable_size0.Add ("300");
            cable_size0.Add ("400");
            cable_size0.Add ("500");
            cable_size0.Add ("600");
            cable_size0.Add ("700");
            cable_size0.Add ("750");

            cb_test_2.DataSource = cable_size0;
            cb_test_2.SelectedIndex = 0;
            cb_test_2.DropDownStyle = ComboBoxStyle.DropDownList;
            //Initial DataBind for cable size ComboBox          
    }

    private void InitializeCableSizeCB1()
    {           
            //Cable List, No Exclusions --------------------------------------------------------------
            cable_size1.Add("Please Select Cable Size");
            cable_size1.Add ("#1");
            cable_size1.Add ("1/0");
            cable_size1.Add ("2/0");
            cable_size1.Add ("3/0");
            cable_size1.Add ("4/0");
            cable_size1.Add ("250");
            cable_size1.Add ("300");
            cable_size1.Add ("400");
            cable_size1.Add ("500");

            cb_test_2.DataSource = cable_size1;
            cb_test_2.SelectedIndex = 0;
            cb_test_2.DropDownStyle = ComboBoxStyle.DropDownList;
            //Initial DataBind for cable size ComboBox          
    }

    private void InitializeSystemTypeCB()
    {
            //System Type List  

            system_type_list.Add("Select System Type");
            system_type_list.Add("3 Phase 3 Wire");
            system_type_list.Add("3 Phase 4 Wire");

            cb_test_3.DataSource = system_type_list;
            cb_test_3.SelectedIndex = 0;
            cb_test_3.DropDownStyle = ComboBoxStyle.DropDownList;
            //Initial DataBind for cb_system type ComboBox              
    }


    private void Button1Click(object sender, System.EventArgs e)
    {   

        if (!String.IsNullOrEmpty(cb_test_1.Text) &&
           (!String.IsNullOrEmpty(cb_test_2.Text) && 
           (!String.IsNullOrEmpty(cb_test_3.Text))))
            {   
                double a;
                if (cb_test_1.SelectedIndex != 0)
                    {
                        x = amperage_dictionary[amperage[cb_test_1.SelectedIndex]];                 
                    }                                                       

                if (cb_test_2.SelectedIndex != 0)
                    {
                        y = cable_dictionary_0[cable_size0[cb_test_2.SelectedIndex]];                   
                    }   

                if (cb_test_3.SelectedIndex != 0)
                    {
                        z = system_type[system_type_list[cb_test_3.SelectedIndex]];
                    }           

                a = ((x / y)*z);
                this.tb_1.Text = Math.Round(a,2).ToString();

            }

    }
    private void InitializeSystemTypeLookup()
    {
        //System Type Dictionary

        this.system_type.Add(this.system_type_list[0], 0);
        this.system_type.Add(this.system_type_list[1], 3);
        this.system_type.Add(this.system_type_list[2], 4);
    }

    private void InitializeAmperageLookup()
    {
        //Amperage Dictionary   

        this.amperage_dictionary.Add(this.amperage[0], 0);
        this.amperage_dictionary.Add(this.amperage[1], 400);
        this.amperage_dictionary.Add(this.amperage[2], 800);
        this.amperage_dictionary.Add(this.amperage[3], 1000);
        this.amperage_dictionary.Add(this.amperage[4], 1200);
        this.amperage_dictionary.Add(this.amperage[5], 1600);
        this.amperage_dictionary.Add(this.amperage[6], 2000);
        this.amperage_dictionary.Add(this.amperage[7], 2500);
        this.amperage_dictionary.Add(this.amperage[8], 3000);
        this.amperage_dictionary.Add(this.amperage[9], 3200);
        this.amperage_dictionary.Add(this.amperage[10], 4000);
        this.amperage_dictionary.Add(this.amperage[11], 5000);
        this.amperage_dictionary.Add(this.amperage[12], 6000);      
    }

    private void InitializeCableLookup0()
    {
        //Cable Dictionary 0 used for cable quantity calculation
        this.cable_dictionary_0.Add (this.cable_size0[0], 0);
        this.cable_dictionary_0.Add (this.cable_size0[1], 130);
        this.cable_dictionary_0.Add (this.cable_size0[2], 150);
        this.cable_dictionary_0.Add (this.cable_size0[3], 175);
        this.cable_dictionary_0.Add (this.cable_size0[4], 200);
        this.cable_dictionary_0.Add (this.cable_size0[5], 230);
        this.cable_dictionary_0.Add (this.cable_size0[6], 255);
        this.cable_dictionary_0.Add (this.cable_size0[7], 285);
        this.cable_dictionary_0.Add (this.cable_size0[8], 355);
        this.cable_dictionary_0.Add (this.cable_size0[9], 380);
        this.cable_dictionary_0.Add (this.cable_size0[10], 720);
        this.cable_dictionary_0.Add (this.cable_size0[11], 475);
    }

private void InitializeCableLookup1()
    {
        //Cable Dictionary 1 used for cable quantity calculation            
        this.cable_dictionary_1.Add (this.cable_size1[0], 0);
        this.cable_dictionary_1.Add (this.cable_size1[1], 130);
        this.cable_dictionary_1.Add (this.cable_size1[2], 150);
        this.cable_dictionary_1.Add (this.cable_size1[3], 175);
        this.cable_dictionary_1.Add (this.cable_size1[4], 200);
        this.cable_dictionary_1.Add (this.cable_size1[5], 230);
        this.cable_dictionary_1.Add (this.cable_size1[6], 255);
        this.cable_dictionary_1.Add (this.cable_size1[7], 285);
        this.cable_dictionary_1.Add (this.cable_size1[8], 355);

    }





















}
名称空间中断
{
部分类主形式
{
双x,y,z;
System.Collections.Generic.List电缆尺寸0;
System.Collections.Generic.List电缆规格1;
//System.Collections.Generic.List电缆规格2;
//System.Collections.Generic.List电缆规格3;
//列出电缆规格4;
//列出电缆规格5;
//列出电缆规格6;
//列出电缆规格7;
//列出电缆规格8;
//列出电缆规格9;
//列出电缆规格10;
System.Collections.Generic.List系统类型列表;
System.Collections.Generic.List安培数;
System.Collections.Generic.Dictionary cable\u Dictionary\u 0;
System.Collections.Generic.Dictionary cable\u Dictionary\u 1;
System.Collections.Generic.Dictionary系统类型;
System.Collections.Generic.Dictionary安培数字典;
私有void MainFormLoad(对象发送方,事件参数)
{
//以下每个列表填充组合框
//要在主窗体上选择的。
安培数=新列表();
系统类型列表=新列表();
//滤器
电缆尺寸0=新列表();
电缆尺寸1=新列表();
//电缆尺寸2=新列表();
//电缆尺寸3=新列表();
//电缆尺寸4=新列表();
//电缆尺寸5=新列表();
//电缆尺寸6=新列表();
//电缆尺寸7=新列表();
//电缆尺寸8=新列表();
//电缆尺寸9=新列表();
//电缆尺寸10=新列表();
系统类型=新字典();
安培数字典=新字典();
//滤器
cable_dictionary_0=新字典();
cable_dictionary_1=新字典();
//cable_dictionary_2=新字典();
//cable_dictionary_3=新字典();
//cable_dictionary_4=新字典();
//cable_dictionary_5=新字典();
//cable_dictionary_6=新字典();
//cable_dictionary_7=新字典();
//cable_dictionary_8=新字典();
//cable_dictionary_9=新字典();
//cable_dictionary_10=新字典();
//
初始化currentloadcb();
初始化SystemTypeCB();
//电缆尺寸过滤器
如果((cb_测试_1.SelectedIndex==0)||
(cb_测试_1.SelectedIndex==1)||
(cb_测试_1.SelectedIndex==2)||
(cb_测试_1.SelectedIndex==3))
{
初始化ablesizecb0();
初始化TableLookup0();
}
//初始化ablesizecb2();
//初始化ablesizecb3();
//初始化ablesizecb4();
//初始化ablesizecb5();
//初始化ablesizecb6();
//初始化ablesizecb7();
//初始化ablesizecb8();
//初始化ablesizecb9();
//初始化ablesizecb10();
//---------------字典---------------------------------------------------------
初始化SystemTypeLookup();
初始化eamperagelookup();
//电缆尺寸过滤器
//初始化TableLookup2();
//初始化TableLookup3();
//初始化TableLookup4();
//初始化TableLookup5();
//初始化TableLookup6();
//初始化TableLookup7();
//初始化EnableLookup8();
//初始化EnableLookup9();
//初始化TableLookup10();
}
私有无效Cb_测试_1已选择索引已更改(对象发送方,事件参数e)
{
如果((cb_测试_1.SelectedIndex==0)||
(cb_测试_1.SelectedIndex==1)||
(cb_测试_1.SelectedIndex==2)||
(cb_测试_1.SelectedIndex==3))
{
初始化ablesizecb1();
}
}
私有void初始化currentloadcb()
{
//电流表,无例外-----------------------------------------------------------
安培数。添加(“请选择安培数”);
安培数增加(“400”);
安培数添加(“800”);
安培数加上(“1000”);
安培数加上(“1200”);
安培数添加(“1600”);
安培数增加(“2000”);
电流加上(“2500”);
电流加上(“3000”);
添加电流(“3200”);
安培数增加(“4000”);
电流加上(“5000”);
电流加上(“6000”);
cb_测试_1.数据源=安培数;
cb_测试_1.SelectedIndex=0;
cb_test_1.DropDownStyle=ComboBoxStyle.DropDownList;
}
私有void初始化ablesizecb0()
{           
//电报单,无例外--------------------------------------------------------------
电缆尺寸0.添加(“请选择电缆尺寸”);
电缆尺寸0.Add(“#1”);
电缆尺寸0.Add(“1/0”);
电缆尺寸0.Add(“2/0”);
电缆尺寸0.Add(“3/0”);
电缆尺寸0.Add(“4/0”);
电缆尺寸0.Add(“250”);
电缆尺寸0.Add(“300”);
电缆尺寸0.Add(“400”);
电缆尺寸0.Add(“500”);
电缆尺寸0.Add(“600”);
电缆尺寸0.Add(“700”);
电缆尺寸0.Add(“750”);
cb_test_2.DataSource=电缆尺寸0;
cb_
if(!IsPostBack)
{
    /* Binding code here */
}
if(!IsPostBack) {
       InitializeCurrentLoadCB();
       InitializeSystemTypeCB();

       //Cable size filter
       if ((cb_test_1.SelectedIndex==0)||
              (cb_test_1.SelectedIndex==1)||
              (cb_test_1.SelectedIndex==2)||
              (cb_test_1.SelectedIndex==3))
       {
              InitializeCableSizeCB0();
              InitializeCableLookup0();
       }
}
private void InitializeCableSizeCB0()
{                       
    //Cable List, No Exclusions
    cable_size0.Clear();
    cable_size0.Add("Please Select Cable Size");
    ...