0

プログラムがデータをインポートするためのファイル パスである複数のテキスト ボックスを含むフォームがあります。現在、長さがゼロでないかどうかは、次の方法でチェックされています。

    //this code imports the files required by the user, as specified in the
    //file path text boxes
    private void btImport_Click(object sender, EventArgs e)
    {
        bool hasPath = false;
        foreach (TextBox box in this.gbPaths.Controls.OfType<TextBox>().Where(tb => tb.Text.Length > 0))
        {
            hasPath = true;
            //import code
        }//end foreach

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

私が疑問に思っているのは、その//import code部分を次のようなものに置き換えることができるかということです:

if(tb.Name = "txtAvF") then...

または同様の、または foreach ループの外でそれを行う必要がありますか? 前もって感謝します。何か明確にする必要がある場合はお知らせください。

4

3 に答える 3

0

The hasPath assignment seems correct to me; it's set for any one text box, and if not set at the end of the loop, a message is displayed. This rhymes well with the text so displayed. Moving the MessageBox call into the loop would cause the dialog box to never be displayed (or errenously displayed), at least as the code is implemented now, since the OfType<>().Where() guarantees that all text boxes iterated over have at least some content.

(I would add this as a comment to @Xaqron but don't have the necessary reputation yet.)

于 2010-10-26T11:30:12.930 に答える
0

TextBox がフォーム上のものの1つであるかどうかを確認したい場合(私はあなただと思います)、あなたはどれです==( MSDNから取得)

the operator == tests for reference equality by determining if two references indicate the same object

だから、これはあなたが探しているものです:

if(box == textBox1 && !string.IsNullOrEmpty(box.Text))
{
      // Import Textbox1
}
else if(box == textBox2 && !string.IsNullOrEmpty(box.Text))
{
      // Import Textbox2
}
else if (box == textBox3....)
于 2010-10-26T03:33:52.150 に答える
0

ループ内で行う必要があります。このような:

if (box.Name == "txtAvF")
    box.Text = "What you want";

ただしhasPath、ループ内で設定すると、最後のパスの状態が保持されます。MessageBoxまた、コードをループ内に移動する必要があります。

于 2010-10-26T03:37:09.670 に答える