试试这个:
var foo = str.Split(' '); //from 111 Simon will get '111'
var id = foo.Length > 1 ? foo[0] : null;
if(id != null) {
//do something with '111';
} else {
//error
}
编辑
我建议您使用自定义Combobox
类来做到这一点:
public class Foo
{
public string Text { get; set; }
public int Value { get; set; }
public Foo(int id, string name)
{
Value = id;
Text = name;
}
public override string ToString()
{
return Text;
}
}
然后将object
(foo) 添加到combobox
:
comboBox1.Items.Add(new Foo(111, "simon"));
然后您可以通过将SelectedItem
属性转换为您的类来访问这些值:
//assuming that comboBox1.SelectedText is "simon", the following is true:
var val = (Foo)comboBox1.SelectedItem;
val.Value // 111
val.Text // simon