在Visual Studio中,我有一个ComboBox
,我手动输入五个项目。 (产品)另外TextBox
我必须自动写出价格。
在数组中,我保存了这些产品和名称的价格:
string [] prodmas = new string[5];
prodmas[0] = "თევზი";
prodmas[1] = "პური";
prodmas[2] = "ყავა";
prodmas[3] = "შაქარი";
prodmas[4] = "წვენი";
double[] fasmas = new double[5];
fasmas[0] = 1.2;
fasmas[1] = 2;
fasmas[2] = 2.4;
fasmas[3] = 1.3;
fasmas[4] = 2.5;
当我在ComboBox
中选择第1项时如何操作,TextBox
必须显示第1项价格(1.2);当我选择item3时TextBox
必须显示第3项价格(2.4)
private void produqcia_SelectedIndexChanged(object sender, EventArgs e)
{
.......
}
FULL CODE
string [] prodmas = new string[5];
double[] fasmas = new double[5];
void masivebi()
{
prodmas[0] = "თევზი";
prodmas[1] = "პური";
prodmas[2] = "ყავა";
prodmas[3] = "შაქარი";
prodmas[4] = "წვენი";
fasmas[0] = 1.2;
fasmas[1] = 2;
fasmas[2] = 2.4;
fasmas[3] = 1.3;
fasmas[4] = 2.5;
}
private void produqcia_SelectedIndexChanged(object sender, EventArgs e)
{
int index = produqcia.SelectedIndex;
fasi.Text = String.Format("The item {0} price {1}", index + 1, fasmas[index].ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
produqcia.DataSource = prodmas;
}
}
答案 0 :(得分:1)
在表单构造函数或其他地方:
comboBox1.DataSource = prodmas;
选择事件:
private void produqcia_SelectedIndexChanged(object sender, EventArgs e)
{
int index = comboBox1.SelectedIndex;
textBox1.Text = String.Format("The item {0} price {1}", index + 1, fasmas[index].ToString());
}
但这不是一个很好的方法,更好地创建包含两个值的对象并将其绑定到组合框。然后将所选项目投射到您的对象并获得所需的值。