컴퓨터/언어,프로그래밍

[C#] 부모창에서 자식창으로 값 넘기는 방법(소스)

스노우볼^^ 2012. 9. 17. 12:50

[부모창] Form1.cs에서

        private void menuAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 ff = new Form2(1);
            ff.Show();            
        }
        private void menuBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 ff = new Form2(2);
            ff.Show();            
        }



[자식창] Form2에서
       
       private int type;
        public Form2(int select)
        {
            type = select;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {         
            if( type == 1)
            {
                this.textBox1.Text = "1";
            }
            else
            {
                this.textBox1.Text = "2";
            }
        }

[결론]
부모창에서 자식창 객체를 생성할 때 인수에 값(변수)을 넣어주고,
자식창에서는 생성자 부분에서 값을 변수에 저장해 두면 값을 사용 할 수 있다. 

[관련글 ] [C#] 자식창에서 부모창으로 값 넘기는 방법(소스) (
http://weezzle.net/3319)