输入的字符串格式不正确 [英] input string is not correct format

查看:282
本文介绍了输入的字符串格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int selcetedComboValue = Int32.Parse(comboBox1.SelectedItem.ToString());

推荐答案

由于错误消息已经提示您,输入不正确,因此无法将其转换为int .

最好的办法是在此代码行上放置一个断点,然后查看选择的comboBox1的值是什么.

根据您的评论,您将获得1-3、5-6之类的值.如果需要两个值,则必须将其包含在两个不同的整数变量中,如下所示

As the error message has already suggested you, the input is not correct and it can not be converted in to an int.

The best thing you can do is put a break-point on this line of code and then see what is the value you have selected comboBox1.

As per your comments, you are having values like 1-3, 5-6. If you want both values, you will have to have it in two different integer variables as shown below

string[] splitValues = Int32.Parse(comboBox1.SelectedItem.Split('-');
int firstValue = Int32.Parse(splitValues[0]);
int firstValue = Int32.Parse(splitValues[1]);



希望有帮助.
Milind



Hope that helps.
Milind


嗨Vinayak,

请注意,根据您的输入,您正在将错误的值转换为整数.

您的组合框值采用以下形式

1-5
6-10
11-20

要将此值限定为整数,需要用分隔符-"将这些值分为两部分.然后您可以将该值转换为整数.


但是问题是您希望从此组合框中获得什么值.
Hi Vinayak,

Please note that you as per your input you are conveting wrong value into integer.

Your combobox values are in the below form

1-5
6-10
11-20

to convet this value into integer you need to split these value into two part by separetor "-". And then you can convert this value into integer.


But the question is what value you want from this combobox.


从要转换的字符串中删除所有非整数字符.如果您有一个包含非int字符的ComboBox项,但需要int部分,则使用一个字符数组,并传递(要转换的字符串的)字符数组,以删除非int字符并存储要转换的最终字符串在一个新变量中.然后转换此仅包含int字符的新字符串.
Remove all non-int characters from the string you are converting. If you have a ComboBox item which contains non-int characters but you need the int part then use a character array and passing the character array(of the string to be converted) remove the non-int characters and store the final string to be converted in a new variable. Then convert this new string you got containing only int-characters.


这篇关于输入的字符串格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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