我有六個文本框,只要有一個有效的輸入,我希望我的程序繼續導入代碼。問題是我不知道最有效的方法是什麼。這兩個方案,我想出迄今如下: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 )到每個文件導入部分,但這是基本要點。
任何幫助表示讚賞:)如果還有其他選項比兩個版本提供的效率都要高,請不要猶豫,直接提交。多謝你們。
'tb.Text.Length'或'tb.TextLength'?不妨使用'string.IsNullOrEmpty()' – PostMan 2010-09-09 02:22:41
對,它應該是tb.Text.Length,沒有使用IDE鍵入。將解決。 – 2010-09-09 02:23:30
沒有IDE IMO的好工作:) :) – PostMan 2010-09-09 02:24:33