2014-04-01 135 views
0

我的程序出現錯誤,我無法解決問題。當我嘗試將用戶輸入分配給我的班級中的變量時,問題就出現了。無法找到錯誤

在問題的兩個變量是:Quantity_Of_Order(串)& Date_Of_Purchase(串)

隨着Quantity_Of_Order,我想給它分配在文本框中輸入的值。 隨着Date_Of_Purchase,我想分配它在日期時間選擇器中選擇的值。

我試圖輸出的值和正在此顯示:

訂購數量:System.Windows.Forms.TextBox,文本:購物滿200 日期:System.Windows.Forms.DateTimePicker,價值:2014年1月4日

當我嘗試使用Quantity_Of_Order,我的程序停止工作,我得到這個消息:

TY未處理的異常PE「System.FormatException」出現在mscorlib.dll 其他信息:輸入字符串的不正確的格式

這裏是我得到的輸入和分配它在我的課的變量代碼:

//This method sets the value of Quantity_Of_Order based on the amount entered by the user 
    void Set_Quantity_Of_Order(int identifier) 
    { 
     string quantity = txtQuantityOrdered.ToString();//txtQuantityOrdered is the text box where user enters value 
     Order[identifier].Quantity_Of_Order = quantity; 
    } 

//This method sets the value of Date_Of_Purchase, based on the date entered by the user 
    void Set_Date_Of_Purchase(int identifier) 
    { 
     string purchase_date = dtpPurchaseDate.ToString();//dtpPurchaseDate is the date time picker where user selects date of purchase. 
     Order[identifier].Date_Of_Purchase = purchase_date; 
    } 

我相信我剛剛做了noob錯誤,因爲我是C#的新手。任何幫助,你可以提供將不勝感激。

感謝

+0

請修改您的標題,使其更具描述性。現在它對你遇到的問題的種類毫不在意。如果你還沒有,請閱讀[問]。 – dandan78

+0

@丹丹對不起,我是新來的,我不知道如何最好地描述這個問題。 – user3478049

回答

4

你不應該使用txtQuantityOrdered.ToString();

txtQuantityOrdered.Text 

而且

dtpPurchaseDate.Text 

或者

dtpPurchaseDate.Value 

最後一個輸出DateTime個對象。

+0

感謝隊友,這似乎已經做到了。我知道我錯過了一些簡單的東西,但是對於它是什麼發揮了精神障礙。真的很感謝幫助。 – user3478049

+0

@ user3478049:沒問題。由於你是新人,如果答案有幫助,請不要忘記將問題標記爲答案。 –