2017-07-16 99 views
-1

我想要輸入的用戶數值並以秒爲單位倒計時,但每當我輸入任何內容並單擊開始按鈕時,它說輸入字符串格式不正確。我用google搜索了一下,並且無法弄清楚如何讓用戶輸入和解析,或者將它轉換爲int和倒計時,同時通過定時器更新標籤。 我使用到控制檯應用程序仍然包裹我周圍的語法頭...C#輸入字符串錯誤

using System; 
using System.Windows.Forms; 

namespace Countdown { 

    public partial class Form1 : Form 
    { 

     int seconds; string user; int test = 30; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void tmrCountdown_Tick(object sender, EventArgs e) 
     { 
      lblDisplay.Text = test.ToString(); 

      if (test > 1) 
      { 
       lblDisplay.Text = test.ToString() + " Seconds Remaining"; 
      } 
      else if (test == 1) 
      { 
       lblDisplay.Text = test.ToString() + " Second Remaining"; 
      } 
      else 
      { 

       tmrCountdown.Stop(); 
      } 
      test--; 
     } 

     public void btnStart_Click(object sender, EventArgs e) 
     { 
      int test = int.Parse(txtBoxInput.Text); 

      tmrCountdown.Start(); 
     } 

     private void txtBoxInput_TextChanged(object sender, EventArgs e) 
     { 

     } 

    } 

} 

錯誤是在「INT測試= int.Parse(txtBoxInput.Text);」

+0

之前任何事情的數字工作(雖然它不會從這個數字倒數),但當然字母/符號錯誤了。有沒有更正確的方法來做到這一點,我試圖學習良好的做法,我嘗試編寫任何代碼甚至每天簡單一次,以提高我的學習速度 –

+0

'int'也有'TryParse'方法,您可能想要看看那個。現在你只是簡單地假設輸入可以被轉換爲一個整數,這當然不總是這樣。如果'int.TryParse'返回false,則向用戶顯示錯誤消息,而不是像現在這樣創建未捕獲的異常。 – oerkelens

回答

0

變化Parse使用全球一流水平的變量爲TryParse,看到無法解析什麼價值:

public void btnStart_Click(object sender, EventArgs e) 
{ 
    if (int.TryParse(txtBoxInput.Text, out test)) 
     // We succeed in parsing, so we continue with the timer 
     tmrCountdown.Start(); 
    else { 
     // We failed in parsing 

     // Let's put keyboard focus on the problem text box... 
     if (txtBoxInput.CanFocus) 
     txtBoxInput.Focus(); 

     // ... and report what's been happened 
     MessageBox.Show($"'{txtBoxInput.Text}' is not a valid integer value", 
         Application.ProductName, 
         MessageBoxButtons.OK, 
         MessageBoxIcon.Warning);  
    } 
} 
+0

謝謝你們兩位,我忘記了需要多少檢查,現在我需要修復我的計時器循環,無論我輸入什麼數字,它都停留在0,也許是因爲int test = 0; hmm –

+0

@Liam Vallance:很對,你應該使用'test'字段,而不是局部變量(我已經放棄了'int test = 0;') –

+0

想到了,現在工作正常。感謝你們所有人提出了一個noob,特別是我提出的所有問題。我確實保留了我在一個片段程序中學到的一切,以幫助我在未來:) –

0

你的代碼有兩個問題。

第一個是你不能保護你的代碼免受無效輸入的事實。如果您沒有在txtBoxInput中輸入某些內容,或者如果鍵入的文本不能轉換爲整數,則會出現無效字符串格式異常

第二個問題是變量測試。您在按鈕單擊內部在本地聲明它,並且假設您沒有得到編譯錯誤,那麼您沒有設置與您在定時器Tick事件中使用的名稱相同的全局類級變量。

因此,每次需要處理用戶輸入時使用TryParse。這在發生問題時不會引發異常,只是返回true或false。最後不要重新聲明按鈕點擊裏面的INT測試變量,而是直接的TryParse

的輸出
public void btnStart_Click(object sender, EventArgs e) 
{ 
    // Try to convert the input in an integer. If this succeed you have 
    // the global variable _test_ set to converted text 
    if(!Int32.TryParse(txtBoxInput.Text, out test) 
     MessageBox.Show("Invalid input. Please type a number!"); 
    else 
     tmrCountdown.Start(); 
} 
0

試試這個代碼在您的按鈕

int test=Convert.ToInt32(txtBoxInput.Text); 
tmrCountdown.Interval = test*1000; //have to multiply to 1000 since timer interval value is in milliseconds. 
tmrCountdown.Start(); 

只要把整數在你的T extbox