将组合框字符串值转换为int [英] Convert combobox string value to int

查看:58
本文介绍了将组合框字符串值转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对转换类型有疑问。我想将当前选择的组合框值字符串更改为int,但是出现错误

I have a question about converting types. I want to change the currently selected combobox value string to an int, but I get errors

我的代码:

int.Parse(age.SelectedItem.ToString());

该问题该怎么办?

推荐答案

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

Ok now we know the error, you can check for a null value before trying to parse it using:

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

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

You can avoid a null value being passed by setting the text property of the control to what ever default value you want.

如果在确保已选择某个值后仍未获得值,则可以确实需要发布您的代码。

If you are still not getting a value after making sure one is selected you really need to post your code.

这篇关于将组合框字符串值转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆