foreach를 이용한 TextBox 초기화

2017. 1. 31. 10:24C# 윈폼 ( Windows Forms )/TextBox

1. MSDN

TextBox 클래스

foreach, in(C# 참조)


2. foreach() 문

foreach() 문


3. foreach를 이용한 TextBox 초기화 방법

   -1) 다음과 같이 GUI를 구성합니다.


   2) Button의 ID를 "btn_TbClear"로 설정하고 클릭이벤트를 추가합니다.


3) 프로그램을 실행 후 버튼을 클릭하면 TextBox가 초기화 됩니다.


   -4) 소스코드

 

namespace yuchae

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void btn_TbClear_Click(object sender, EventArgs e)

        {

            foreach (Control ControlTextBoxClear in this.Controls)

            {

                if (typeof(TextBox) == ControlTextBoxClear.GetType())

                {

                    (ControlTextBoxClear as TextBox).Text = ""; 

                }

            }

            MessageBox.Show("앙큼한유채 : TextBox 초기화", "Information");

        }

    }

}