0

グループボックス内のすべてのコントロールをループし、テキストが含まれているすべてのコントロールを見つけてタブストップ プロパティを false に設定するループを作成しようとしています。ただし、コントロールにテキストがある場合でも、一部のコントロールの tabstop プロパティは常に true でなければなりません。

これは私のコードです:

    foreach (Control c in deliveryGroup.Controls)
    {
        if (c is Label || c is Button)
        {
            c.TabStop = false;
        }
        else
        {
            if (!string.IsNullOrEmpty(c.Text))
            {
                c.TabStop = false;
            }
            else if (c.Name == "cmbPKPAdrID")
            {

            }
            else if (c.Name.ToString() == "cmbPKPType")
            {
                c.TabStop = true;  <<------- never enters here
            }
            else if (c.Name.ToString() == "dtpPKPDate")
            {
                c.TabStop = true;  <<------- never enters here
            }
            else
            {
                c.TabStop = true;
            }
        }
    }

私の問題は、プログラムは実行されますが、矢印でマークしたコードに実行されないことです。コントロールに特定の名前がある場合は、タブストッププロパティをtrueに設定したいのに、飛び出してタブストッププロパティをfalseに設定します。

私は何を間違っていますか?

4

1 に答える 1

2

コード行は

if (!string.IsNullOrEmpty(c.Text))

TabStopfalseに設定したくないコントロールに対して実行されており、そのコントロールには現在テキストが含まれています。

これを修正するには、次のようにテストの順序を変更します。

foreach (Control c in deliveryGroup.Controls)
{
    if (c is Label || c is Button)
    {
        c.TabStop = false;
    }
    else
    {
        if (c.Name == "cmbPKPAdrID")
        {

        }
        else if (c.Name == "cmbPKPType")
        {
            c.TabStop = true;
        }
        else if (c.Name == "dtpPKPDate")
        {
            c.TabStop = true;
        }
        else if (!string.IsNullOrEmpty(c.Text))
        {
            c.TabStop = false;
        }
        else
        {
            c.TabStop = true;
        }
    }
}

これに単純化できます:

foreach (Control c in deliveryGroup.Controls)
{
    if (c is Label || c is Button)
    {
        c.TabStop = false;
    }
    else
    {
        if (c.Name == "cmbPKPAdrID")
        {

        }
        else if (c.Name == "cmbPKPType")
        {
            c.TabStop = true;
        }
        else if (c.Name == "dtpPKPDate")
        {
            c.TabStop = true;
        }
        else
        {
            c.TabStop = string.IsNullOrEmpty(c.Text);
        }
    }
}
于 2013-06-17T08:50:08.343 に答える