我還有一個關於TryParse方法的問題,我已經創建它來檢查我的用戶輸入。對不起,我的額外問題,但我碰到了另一個複雜因素,並希望得到更多的幫助,所以我提出了另一個問題,因爲我的最後一個是很老的。如果我把它發佈在最後一個,我擔心沒有人會看到這個問題。當輸入一個十進制時Decimal TryParse失敗
沒有錯誤,但是當我嘗試運行它來測試我的用戶輸入時,對於我輸入的所有內容(包括整數,小數點(1.00,1.0,1000.0)),它總是給我Messagebox.Show。下面是我創建:
{
// Arrange the variables to the correct TextBox.
decimal Medical;
if (!decimal.TryParse(ChargeLabel.Text, out Medical))
{
MessageBox.Show("Please enter a decimal number.");
}
decimal Surgical;
if (!decimal.TryParse(ChargeLabel.Text, out Surgical))
{
MessageBox.Show("Please enter a decimal number.");
}
decimal Lab;
if (!decimal.TryParse(ChargeLabel.Text, out Lab))
{
MessageBox.Show("Please enter a decimal number.");
}
decimal Rehab;
if (!decimal.TryParse(ChargeLabel.Text, out Rehab))
{
MessageBox.Show("Please enter a decimal number.");
}
// Find the cost of Miscs by adding the Misc Costs together.
decimal MiscCharges = Medical + Surgical + Lab + Rehab;
ChargeLabel.Text = MiscCharges.ToString("c");
換句話說,我試着輸入任何形式的內科,外科,實驗室和康復文本框的數字,它仍然給了我同樣的消息框。有人會幫助我如何讓我的應用程序正確檢查用戶的輸入嗎?謝謝你,再次抱歉。
請推動一些輸入。它看起來很可能不是你所期望的。因此,當你確定要執行輸入時,請確保從調試會話中獲得它(而不是你的時間) – 2013-03-17 18:51:47
只需觀察一下,如果用戶決定不再次輸入小數,會發生什麼?例如消息框出現提示小數點,並且用戶決定反對它......在第二次提示後沒有檢查完成嗎? – Sam 2013-03-17 18:55:43
你在什麼時候進行檢查?也許ChargeLabel.Text是空的? – alex 2013-03-17 18:59:06