简体   繁体   English

根据C#中组合框选择中的字符串存储整数

[英]Store an integer based on a string from a combo box selection in C#

What I'm trying to do here is to select a string in comboBox1 and comboBox2, have an integer that is entered into to textBox1 multiplied by a specific number based on the selections made in the comboBoxs and then output the results of that multiplication to another read-only textbox. 我在这里想要做的是在comboBox1和comboBox2中选择一个字符串,将一个整数输入到textBox1中,并乘以基于comboBoxs中所做选择的特定数字,然后将该乘积的结果输出到另一个只读文本框。

comboBox1 has: comboBox1具有:
if comboBox1 = "Alpha" use integer 170 如果comboBox1 =“ Alpha”使用整数170
if comboBox1 = "Bravo" use integer 185 如果comboBox1 =“ Bravo”使用整数185
if comboBox1 = "Charlie" use integer 195 如果comboBox1 =“ Charlie”使用整数195
if comboBox1 = "Delta" use integer 225 如果comboBox1 =“ Delta”使用整数225

& comboBox2 has: &comboBox2具有:
if comboBox2 = "New" add 0 to the integer value determined in comboBox1 如果comboBox2 =“ New”,则将comboBox1中确定的整数值加0
comboBox2 = "Old" add 25 to the integer value determined in comboBox1 comboBox2 =“ Old”将25加到comboBox1中确定的整数值

Multiply a user defined integer entered in textBox1 by the sum value determined above and output that integer into the read-only textBox. 将在textBox1中输入的用户定义的整数乘以上面确定的总和,然后将该整数输出到只读textBox中。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

I would either use a Tuple<string, int> or a class for the items in the list. 我将使用Tuple<string, int>或一个用于列表中项目的类。 Maybe something like: 也许像:

class CBItem
{
    public string Text { get; private set; }
    public int Value { get; private set; }

    public CBItem(string text, int value)
    {
        Text = text;
        Value = value;
    }

    // This will determine what you see in the combobox
    public override string ToString()
    {
        return Text ?? base.ToString();
    }
}

Then you can add a bunch of CBItem s to your combobox instead of just strings: 然后,您可以向组合框添加一堆CBItem ,而不仅仅是字符串:

comboBox1.Items.Add(new CBItem("Alpha", 170));
comboBox1.Items.Add(new CBItem("Bravo", 185));
comboBox1.Items.Add(new CBItem("Charlie", 195));
comboBox1.Items.Add(new CBItem("Delta", 225));

comboBox2.Items.Add(new CBItem("New", 0));
comboBox2.Items.Add(new CBItem("Old", 25));

And then in your handler, you can just cast SelectedItem to type CBItem. 然后在处理程序中,只需将SelectedItem强制转换为CBItem类型即可。

CBItem cb1Item = (CBItem)comboBox1.SelectedItem;
CBItem cb2Item = (CBItem)comboBox2.SelectedItem;

int sum = cb1Item.Value + cb2Item.Value;

You can figure out how to multiply that sum by the value entered in the textbox from there. 您可以弄清楚如何将该总和乘以从此处在文本框中输入的值。

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

相关问题 C#Windows窗体如何根据第一个组合框中的选择更改第二个组合框的值 - C# Windows Forms how to change values of second combo box based on selection in first combo box C#将字符串从组合框转换为Int并将其传递 - C# Convert String from Combo Box to Int and pass it C#使用仅1个选择的txt文件中的值填充2个组合框和1个文本框 - C# populating 2 combo boxes and 1 text box with values from a txt file with only 1 selection 根据组合框 c# 的选择,我的代码没有切换到所需的 function - My code is not switching to the desired function depending upon the selection from combo box c# C#从方法填充组合框 - C# populate a combo box from a method 如何根据从另一个组合框所做的选择填充组合框 - How to populate a combo box based on the selection made from another combo box C#如何根据使用字符串保存的属性设置来设置组合框值 - C# how to set a combo box value based on saved property settings using string 如何从SQL数据库获取数据以存储在组合框中-C# - How to get data from SQL database to store in combo box - C# 如何从SQL数据库中获取数据以存储到组合框中 - C# - How to get data from SQL database to store in to a combo box - C# 如何从字符串中分离整数值并将其存储到C#中的变量中 - how to seperate integer values from a string and store it into a variable in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM