2017-07-16 116 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