2017年4月7日金曜日

ASP.netで全てのコントロールの背景色・内容クリア・Enable・Visibleを変更する方法 [C#]

変更メソッド
------------------------------------------------------------------
 public static void ClearTextBox(Control controls)
{
    //コントロールがテキストボックスの場合
    if (controls is TextBox)
    {
        TextBox tb = (TextBox)controls;

        //テキストをクリアする場合
        tb.Text = "";
       
        //背景色を変更する場合
        tb.BackColor = SystemColors.Window;
       
        //Enabledを変更する場合
        tb.Enabled = true;

        //Visibleを変更する場合
        tb.Visible = true;
    }

    //自分にコントロールが含まれているか
    if (controls.HasControls())
    {
        //自分に含まれているコントロールのループ
        foreach (Control cControl in controls.Controls)
        {
            ClearTextBox(cControl);
        }
    }
}
------------------------------------------------------------------

呼び出し側
------------------------------------------------------------------
ClearTextBox(this)
------------------------------------------------------------------

画面の特定の部分だけに適用したい場合は、
画面にPanelを配置してパネルを投げればPanel内のTextBoxにだけ適用される。
------------------------------------------------------------------
EnableTextBox(Panel1)
------------------------------------------------------------------

0 件のコメント:

コメントを投稿