2016-05-12 47 views
0
System.InvalidCastException: Unable to cast object of type 'System.String' to type 'Cookie.Dozen'. at Cookie.CookieSource.listBox1_SelectedIndexChanged(Object sender, EventArgs e) in Form1.cs:line 112 

線112以下:爲什麼我得到這個錯誤窗口形式?

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    this.dozen = (Dozen) this.listBox1.SelectedItem; 
    this.CostChosenLb.ResetText(); 
} 

我想要做的是,當我選擇從列表框中的項目之一,它會在文本框中顯示的價格。但每當我嘗試這樣做時,我都會遇到上述錯誤。幫幫我!

+0

什麼是'this.dozen'的類型 – Hexie

回答

0

你的列表框綁定到一個字符串類型而不是一打類型。你應該檢查你如何設置ListBox Items屬性。

this.listBox1.Items = yoursList 

您可以嘗試更改您的代碼:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    this.dozen = this.listBox1.SelectedItem as Dozen; 
    this.CostChosenLb.ResetText(); 
} 
相關問題