0

こんにちは、おそらく単純なものです。C# .Net 4.0 と Visual Studio 2012 Ultimate を使用。

次のコードを取得しました。

string part = "";
part = txtIOpart.Text;
txtBatchCV.Text = txtBatchIO.Text;
txtPartCV.Text = part;
txtExternalCV.Text = Sqlrunclass.SplitSpec_External(part, pg);
txtInternalCV.Text = Sqlrunclass.SplitSpec_Internal();
txtABSCV.Text = Sqlrunclass.SplitSpec_cvABS();
txtOilCV.Text = Sqlrunclass.SplitSpec_OilSeal();

txtBarCV.Text = "*" + Sqlrunclass.SplitInfo_ASno(part, pg) + "*";
txtBarNumCV.Text = txtBarCV.Text;
txtLocnCV.Text = Sqlrunclass.SplitInfo_Location();
txtFitsCV.Text = Sqlrunclass.SplitInfo_Desc();
txtHeightCV.Text = Sqlrunclass.SplitSpec_Height();
txtDiameterCV.Text = Sqlrunclass.SplitSpec_Diameter();
txtCirclitCV.Text = Sqlrunclass.SplitSpec_Circlit();

picTypeCV.Image = ftpclass.Download("CVspecType" + Sqlrunclass.SplitSpec_TypeCV() + ".jpg", "ftp.shaftec.com/Images/TypeJpg", "0095845|shafteccom0", "4ccc7365d4");

if (txtBatchCV.Text == null || txtBatchCV.Text == "")
{
    txtBatchCV.Text = "ALL";
}

下部にあるように、バッチをチェックしていますが、一連のメソッドによって設定されているすべてのデータをチェックする必要があります。null または空白の txt が表示された場合、それぞれに異なる txt 出力が表示されます。とにかくこのコードを短くする方法はありますか?

4

6 に答える 6

3

txtBatchCV.Textたとえば、試してみてください

//Just for null
txtBatchCV.Text = (txtBatchCV.Text ?? "ALL").ToString(); 

//for both null and empty string
txtBatchCV.Text = string.IsNullOrEmpty(txtBatchCV.Text) ? "ALL": txtBatchCV.Text; 
于 2012-12-14T14:19:18.250 に答える
3

すべてのテキストボックスを反復処理できます

foreach (var txt in form.Controls.OfType<TextBox>())
{
    switch(txt.Id){
        case "txtBatchCV":
        // Do whatever you want for txtBatchCV e.g. check string.IsNullOrEmpy(txt.Text)
        break;
    }
}

以上、以下からお借りしました。

すべてのテキスト ボックスをループして、アクション ディクショナリから対応するアクションを実行するにはどうすればよいですか?

Tim からのコメントに応えて、何ができるかを説明するコードをもう少し追加しました。私のコード例は、完全なソリューションを意図したものではありません。

于 2012-12-14T14:21:18.483 に答える
1

手始めに使用できるstring.IsNullOrEmpty(txtBatchCV.Text)のは、基本的にifチェックで行うことを行うコンビニエンスメソッドです。

于 2012-12-14T14:19:28.063 に答える
1

私はこのようなことを試みます:

void SetDefaultIfNull(TextBox txt, string defaultVal)
{
    if (string.IsNullOrWhitespace(txt.Text))
        txt.Text = defaultVal;
}

次に、各テキストボックスとデフォルトをメソッドに渡します。

于 2012-12-14T14:20:39.760 に答える
1

少なくとも次のいずれかの方法を使用できます。

string.IsNullOrEmpty(txtBatchCV.Text)

また

string.IsNullOrWhitespace(txtBatchCV.Text)

于 2012-12-14T14:20:42.787 に答える
1

TextBox.Textではありませんnull。その場合は戻り""ます。メソッドが返されたnull場合は、次を使用できますnull-coalescing operator

string nullRepl = "ALL";
txtExternalCV.Text = Sqlrunclass.SplitSpec_External(part, pg) ?? nullRepl;
txtInternalCV.Text = Sqlrunclass.SplitSpec_Internal() ?? nullRepl;
txtABSCV.Text = Sqlrunclass.SplitSpec_cvABS() ?? nullRepl;
txtOilCV.Text = Sqlrunclass.SplitSpec_OilSeal() ?? nullRepl;
txtLocnCV.Text = Sqlrunclass.SplitInfo_Location() ?? nullRepl;
txtFitsCV.Text = Sqlrunclass.SplitInfo_Desc() ?? nullRepl;
txtHeightCV.Text = Sqlrunclass.SplitSpec_Height() ?? nullRepl;
txtDiameterCV.Text = Sqlrunclass.SplitSpec_Diameter() ?? nullRepl;
txtCirclitCV.Text = Sqlrunclass.SplitSpec_Circlit() ?? nullRepl;
于 2012-12-14T14:26:24.527 に答える