[英]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.