Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#字典应用:通过选择键应用值_C#_Dictionary_Combobox - Fatal编程技术网

C#字典应用:通过选择键应用值

C#字典应用:通过选择键应用值,c#,dictionary,combobox,C#,Dictionary,Combobox,我有一本字典,我用它来定义铜缆的额定值。我想使用这些值来计算满足特定连接额定值所需的电缆数量。连接尺寸、电缆类型和系统类型由两个名为cb_amperage、cb_cable_size和cb_system_type的组合框选择。方程运行后,找到的答案将显示在名为tb6_cable_qty的文本框中。我欢迎任何意见和建议。提前感谢您的帮助 数学很简单: decimal x, y, z; x = decimal.Parse(cb_amperage.); y = decimal.Parse();//&l

我有一本字典,我用它来定义铜缆的额定值。我想使用这些值来计算满足特定连接额定值所需的电缆数量。连接尺寸、电缆类型和系统类型由两个名为cb_amperage、cb_cable_size和cb_system_type的组合框选择。方程运行后,找到的答案将显示在名为tb6_cable_qty的文本框中。我欢迎任何意见和建议。提前感谢您的帮助

数学很简单:

decimal x, y, z;
x = decimal.Parse(cb_amperage.);
y = decimal.Parse();//<---- this value must come from the dictionary below
a = decimal.Parse();//<---- this value must also come from a dictionary below
z = (x / y) * a
tb6_cable_qty.Text = Math.Round(z,2).ToString();


void Cb_amperageSelectedIndexChanged(object sender, EventArgs e)    
    { 
        if (!String.!IsNullOrEmpty(cb_amperage) & !String.IsNullOrEmpty(cb_cable_size))
        //not sure if the above is right but I think it coveys the idea
       {
        //function based on the values in the dictionary below
       }

        //Cable Dictionary 1 used for cable quantity calculation
        Dictionary<string, int> cable_dictionary_1 = new Dictionary<string, int>();

        cable_dictionary_1.Add ("#1", 130);
        cable_dictionary_1.Add ("1/0", 150);
        cable_dictionary_1.Add ("2/0", 175);
        cable_dictionary_1.Add ("3/0", 200);
        cable_dictionary_1.Add ("4/0", 230);
        cable_dictionary_1.Add ("250", 255);
        cable_dictionary_1.Add ("300", 285);
        cable_dictionary_1.Add ("400", 355);
        cable_dictionary_1.Add ("500", 380);
        cable_dictionary_1.Add ("600", 720);
        cable_dictionary_1.Add ("750", 475);


        //System Type Dictionary used for cable quantity calculation
        Dictionary<string, int> system_type_dictionary = new Dictionary<string, int>();

        system_type_dictionary.Add ("3P 3W", 3);
        system_type_dictionary.Add ("3P 4W", 4);
十进制x,y,z;
x=十进制解析(cb_安培数);

y=decimal.Parse()// 我不确定你的问题是什么?听起来你已经把一切都搞定了。

编辑:等等,现在我明白你的问题了

下面是我要做的:

首先,对于每个键,将它们放入字符串列表中,以便:

private const string mFirst = "#1";
然后,将以下内容添加到词典中:

mDictionary.Add(mFirst, 130); //etc...
然后,为每个组合框添加字符串

comboBox1.Add(mFirst);
comboBox1.Add(mSecond); //etc...
然后,将事件添加到组合框“选定索引已更改”:

comboBox1_SelectedIndexChanged(SomeEventStuff I Forget)
当值在那里更改时,更新y的值。对comboBox2执行相同的操作,并更新z的值。更新任何一个值后,只需同时更新结果即可

编辑2:

要进行计算,我需要一个如下的函数:

float CopperCalc(float inX, float inY, float inA){
     if (inY != 0){//avoid the division by zero
         return (inX / inY) * inA;
     } else {
         return 0; //just assume it's zero here.
     }//I'm not Jon Skeet, I can't divide by zero
}
然后,在selectedindexchanged处理程序中调用该函数,并使用它更新文本框:

 //put your mX, mY, and mA as member values of your class, and then
 //they get updated.  For instance:
 void comboBox1_SelectedIndexChanged(Events){
     mX = mDictionary[comboBox1.SelectedItem];
     textBox1.Text = CopperCalc(mX, mY, mA).ToString("f1");
 }
并为每个组合框放置相同的函数,这样组合框更新时,文本框也会更新。 只有当您想尝试自动确定导线的组合时,这一点才有意义:

这听起来像是…的变体,对吗?您需要找出一些最小的铜线组合,以满足连接要求?是否存在一些最小值或最大值或其他约束?为什么不扔掉大量的铜来满足最小的需求呢

这是一个非常冗长的说法,我想我需要更多的信息来理解这个问题;但是,如果背包问题是一个合理的类比,那么有解决方案,但它们是NP完全实现的。也就是说,它们不会“快速”运行,但如果您只有12个线规(或其他什么),您的速度可能会很好。

解决了!!!! 下面是代码。感谢MSDN C#论坛的Rudedog2

部分类MainForm
{
双x,y,z;
列出电缆规格1;
列表系统\类型\列表;
表电流;
字典电缆\u字典\u 1;
字典系统(u型),;
字典安培数字典;
私有void MainFormLoad(对象发送方,事件参数)
{
//以下每个列表填充组合框
//要在主窗体上选择的。
安培数=新列表();
电缆尺寸1=新列表();
系统类型列表=新列表();
cable_dictionary_1=新字典();
系统类型=新字典();
安培数字典=新字典();
//
初始化currentloadcb();
初始化ablesizebcb();
初始化SystemTypeCB();
//---------------字典---------------------------------------------------------
初始化SystemTypeLookup();
InitializeCableLookup();
初始化eamperagelookup();
}
私有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;
}
private void InitializeTableSizeECB()
{
//电报单,无例外--------------------------------------------------------------
电缆尺寸1=新列表();
电缆尺寸1.添加(“请选择电缆尺寸”);
电缆尺寸1.Add(“#1”);
电缆尺寸1.Add(“1/0”);
电缆规格1.Add(“2/0”);
电缆规格1.Add(“3/0”);
电缆规格1.Add(“4/0”);
电缆规格1.Add(“250”);
电缆规格1.Add(“300”);
电缆规格1.Add(“400”);
电缆规格1.Add(“500”);
电缆规格1.Add(“600”);
电缆规格1.Add(“700”);
电缆规格1.Add(“750”);
cb_测试_2.数据源=电缆尺寸1;
cb_测试_2.SelectedIndex=0;
cb_test_2.DropDownStyle=ComboBoxStyle.DropDownList;
//电缆尺寸组合框的初始数据绑定
}
私有void初始化SystemTypeCB()
{
//系统类型列表
系统类型列表=新列表();
系统类型列表。添加(“选择系统类型”);
系统类型列表。添加(“3相3线”);
系统类型列表。添加(“3相4线”);
cb_test_3.DataSource=系统类型列表;
cb_测试_3.SelectedIndex=0;
cb_test_3.DropDownStyle=ComboBoxStyle.DropDownList;
//cb_系统类型组合框的初始数据绑定
}
私有无效按钮1单击(对象发送者,System.EventArgs e)
{   
如果(!String.IsNullOrEmpty(cb_test_1.Text)&&
(!String.IsNullOrEmpty(cb_test_2.Text)&&
(!String.IsNullOrEmpty(cb_test_3.Text)))
{   
双a;
我
 //put your mX, mY, and mA as member values of your class, and then
 //they get updated.  For instance:
 void comboBox1_SelectedIndexChanged(Events){
     mX = mDictionary[comboBox1.SelectedItem];
     textBox1.Text = CopperCalc(mX, mY, mA).ToString("f1");
 }
partial class MainForm
{
    double x, y, z;     

    List<string> cable_size1;
    List<string> system_type_list;
    List<string> amperage;

    Dictionary<string, double> cable_dictionary_1;
    Dictionary<string, double> system_type;
    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>();
            cable_size1 = new List<string>();
            system_type_list = new List<string>();          

            cable_dictionary_1 = new Dictionary<string, double>();
            system_type = new Dictionary<string, double>();
            amperage_dictionary = new Dictionary<string, double>();

            //
            InitializeCurrentLoadCB();
            InitializeCableSizeCB();
            InitializeSystemTypeCB();

            //---------------Dictionaries---------------------------------------------------------
            InitializeSystemTypeLookup();
            InitializeCableLookup();
            InitializeAmperageLookup();
    }
    private void InitializeCurrentLoadCB()
    {
            //Amperage List, No Exclusions-----------------------------------------------------------
            amperage = new List<string>();
            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 InitializeCableSizeCB()
    {
            //Cable List, No Exclusions --------------------------------------------------------------
            cable_size1 = new List<string>();
            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");
            cable_size1.Add ("600");
            cable_size1.Add ("700");
            cable_size1.Add ("750");

            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 = new List<string>();
            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_1[cable_size1[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 = new Dictionary<string, double>();
        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 InitializeCableLookup()
    {
        //Cable Dictionary 1 used for cable quantity calculation
        this.cable_dictionary_1 = new Dictionary<string, double>();
        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);
        this.cable_dictionary_1.Add (this.cable_size1[9], 380);
        this.cable_dictionary_1.Add (this.cable_size1[10], 720);
        this.cable_dictionary_1.Add (this.cable_size1[11], 475);
    }
    private void InitializeAmperageLookup()
    {
        //Amperage Dictionary   
        this.amperage_dictionary = new Dictionary<string, double>();
        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);      
    }


}