将组合框字符串值转换为int

asl*_*slı 3 c# combobox

我有关于转换类型的问题.我想将当前选中的组合框值字符串更改为int,但是我收到错误

我的代码:

int.Parse(age.SelectedItem.ToString());
Run Code Online (Sandbox Code Playgroud)

我能为这个问题做些什么?

Mat*_*att 6

好的,现在我们知道了错误,您可以在尝试使用以下方法解析之前检查空值:

    if (comboBox1.SelectedItem != null)
    {
        int x = int.Parse(comboBox1.SelectedItem.ToString());
    }
    else { //Value is null }
Run Code Online (Sandbox Code Playgroud)

您可以通过将控件的text属性设置为所需的默认值来避免传递null值.

如果在确定选择了某个值后仍未获得值,则确实需要发布代码.