2014-12-06 45 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))