-1
我想获得列表框中所有值的总和,我想在文本框中显示结果。当我运行该程序并单击该按钮时,出现以下错误:System.InvalidCastException:'指定的转换无效。'尝试添加并显示ListBox中的值的总和...获取错误:System.InvalidCastException:'指定的转换无效。'
private void readButton_Click(object sender, EventArgs e)
{
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Harra\Documents\Visual Studio 2017\Projects\File Reader\Sales.txt");
while ((line = file.ReadLine()) != null)
{
displayListBox.Items.Add(line);
counter++;
}
{
int intTotal = 0;
int intCounter;
double dblAdd;
for (intCounter = 0; intCounter <= displayListBox.Items.Count - 1; intCounter++)
{
intTotal += Convert.ToInt32(displayListBox.Items[intCounter];
}
dblAdd = (double)intTotal;
//trying to display total to textbox
totalTextBox.Text = string.Format("{0:F}", dblAdd);
}
}
您好,感谢您的答复! 我仍然收到错误消息:System.FormatException:'输入字符串的格式不正确。' – Relaxsingh
那么,你的列表框项目是不是整数,然后呢?给出一个你的列表框项目值的例子(来自sales.txt文件的行) –
这里有两个例子:1245.67,1189.55 – Relaxsingh