-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