0

I just started learning c# a week ago. I have a little experience with objective c. So give me a hard time for asking such a beginner question.

Well, I would like the user to be able to select from the combo box. I created the combobox visually in xaml. The contents of the combo box are strings. However, when the user selects their string of choice, I would like store a value to an int so I can do calculations. The errors I have been can't convert object to string and many other conversation errors.

int x;
string selValue = (string)comboBox1.SelectedValue;
if ((selValue=="green")||(selValue=="blue"))
{ 
    x= 5; 
}

Would it be best to create a function that accepts an object and returns an int, if so please tell ?

4

2 回答 2

0

SelectedValue属性将返回ComboBoxItemasobject并将作为索引SelectedIndex返回。ComboBoxIteminteger

于 2013-04-09T05:54:58.090 回答
0

您应该将字符串绿色作为选定文本,并将值 int 5 作为选定值,这样您就可以删除所有代码并执行 int x = (int)comboBox1.SelectedValue,这将为您提供 5。您需要谷歌 winform 组合框数据绑定,您会看到可以将不同的属性绑定到文本和值。

于 2013-04-08T22:08:51.367 回答