2010-09-09 36 views
1

我有六個文本框,只要有一個有效的輸入,我希望我的程序繼續導入代碼。問題是我不知道最有效的方法是什麼。這兩個方案,我想出迄今如下:C# - 多文本框非零長度驗證效率

版本1:

 //if no paths are specified the user is shown an error and the program 
     //will do nothing 
     if ((txtForecastFTE.Text.Length != 0) 
      || (txtActualFTE.Text.Length != 0) 
      || (txtForecastAHT.Text.Length != 0) 
      || (txtActualAHT.Text.Length != 0) 
      || (txtForecastVolume.Text.Length != 0) 
      || (txtActualVolume.Text.Length != 0)) 
     { 
      if (txtForecastFTE.Text.Length != 0) 
      { 
       //import code 
      }//end if 

      if (txtActualFTE.Text.Length != 0) 
      { 
       //import code 
      }//end if 

      if (txtForecastAHT.Text.Length != 0) 
      { 
       //import code 
      }//end if 

      if (txtActualAHT.Text.Length != 0) 
      { 
       //import code 
      }//end if 

      if (txtForecastVolume.Text.Length != 0) 
      { 
       //import code 
      }//end if 

      if (txtActualVolume.Text.Length != 0) 
      { 
       //import code 
      }//end if 
     }//end if 
     else 
     { 
      MessageBox.Show("You must enter the path for at least one file."); 
     }//end if-else 
    }//end import code 

版本2:

 //if no paths are specified the user is shown an error and the program 
     //will do nothing 
     if (txtForecastFTE.Text.Length != 0) 
     { 
    pathTrue = true; 
      //import code 
     }//end if 

     if (txtActualFTE.Text.Length != 0) 
     { 
    pathTrue = true; 
      //import code 
     }//end if 

     if (txtForecastAHT.Text.Length != 0) 
     { 
    pathTrue = true; 
      //import code 
     }//end if 

     if (txtActualAHT.Text.Length != 0) 
     { 
    pathTrue = true; 
      //import code 
     }//end if 

     if (txtForecastVolume.Text.Length != 0) 
     { 
    pathTrue = true; 
      //import code 
     }//end if 

     if (txtActualVolume.Text.Length != 0) 
     { 
    pathTrue = true; 
      //import code 
     }//end if 

     if (!pathTrue) 
     { 
      MessageBox.Show("You must enter the path for at least one file."); 
     }//end if 
    }//end import code 

很顯然,我會加入進一步驗證(的try-catch )到每個文件導入部分,但這是基本要點。

任何幫助表示讚賞:)如果還有其他選項比兩個版本提供的效率都要高,請不要猶豫,直接提交。多謝你們。

回答

2

在這種情況下,我認爲「效率」不應該是您的目標,而應該是簡單,可讀,非冗餘的代碼。

if (this.Controls.OfType<TextBox>().Any(tb => tb.Text.Length > 0)) 
{ 
    // get to work 
} 

另一種方法,這可能是在你的樣品的檢測更加貼合,可能是

bool hasPath = false; 
foreach (TextBox box in this.Controls.OfType<TextBox>().Where(tb => tb.Text.Length > 0)) 
{ 
    hasPath = true; 
    // import code 
} 

if (!hasPath) 
{ 
    MessageBox.Show("Need data"); 
} 
+0

'tb.Text.Length'或'tb.TextLength'?不妨使用'string.IsNullOrEmpty()' – PostMan 2010-09-09 02:22:41

+0

對,它應該是tb.Text.Length,沒有使用IDE鍵入。將解決。 – 2010-09-09 02:23:30

+0

沒有IDE IMO的好工作:) :) – PostMan 2010-09-09 02:24:33

0

我會建議你把這些文本框在初始化時的私人List<TextBox>變量,並執行安東尼解釋說同樣的事情。

我不是控制財產的粉絲,因爲有時候你的孩子控制不立即孩子你Form/UserControl(例如嵌套在GroupBox)。如果你枚舉了Controls屬性來做某些事情,並且稍後移動控件並將它們放置在嵌套容器中,那麼代碼將失去魔力,有時很難意識到發生了什麼。因此,我更願意明確地確定它們。