2014-12-06 43 views
0

我创建了一个Windows窗体应用程序,它有一个TextBox,您可以在其中输入一个由我的程序修改的数字。如何跳跃周期如果?

我创建了if子句,如果TextBox.Text为空,我不想运行计算。但即使我将TextBox留空,由于没有任何价值,异常情况也会上升。如何跳过此if条款?

private void button1_Click(object sender, EventArgs e) 
     { 

      string strMP3Folder = @"C:\Users\Stefano\Music\shake.mp3"; 

      string strMP3OutputFilename = @"C:\Users\Stefano\Music\funziona2.mp3"; 

      using (Mp3FileReader reader = new Mp3FileReader(strMP3Folder)) 
      { 
       int count = 1; 
       Mp3Frame mp3Frame = reader.ReadNextFrame(); 
       System.IO.FileStream _fs = new System.IO.FileStream(strMP3OutputFilename, System.IO.FileMode.Create, System.IO.FileAccess.Write); 

       double valoreIniziale = 0; 
       if (textBox2.Text != null) 
       { 
        valoreIniziale = Convert.ToDouble(textBox2.Text); 
       } 

       double valorefinale = 1000000; 
       if (textBox1.Text != null) 
       { valorefinale = Convert.ToDouble(textBox1.Text); } 

回答

0

当您检索TextBox.Text属性时,“吸气”运行下面的代码:

return (text == null) ? "" : text; 

Text财产从未返回null ,所以你的代码和你现在的总是执行一样。

相反,空字符串只是测试:

if (textBox2.Text != "") 
{ 
    // do something 
} 
3

第一件事“if”不是循环。其次,如果我理解正确,你想忽略“if”块,以防文本框内的文本为空或为空。如果这是你想要的,你可以做什么:

if(!String.IsNullOrEmpty(textBox1.Text))