Software

폼간 데이터 전달(인터페이스 | 델리게이트)

GreatDragin.Kim 2016. 5. 19. 00:51
반응형


1. 인터페이스 이용


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace FormTransfer

{

    public interface IMyInterface

    {

        void getResult(int a, int b);

    }

 

    public partial class Form1 : Form, IMyInterface

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        public void getResult(int a, int b)

        {

            int r = a + b;

            label1.Text = r.ToString();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            Form2 f2 = new Form2(this as IMyInterface);

            f2.Show();

        }

    }

}


 

 using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace FormTransfer

{

    public partial class Form2 : Form

    {

        private IMyInterface f1 = null;

        public Form2()

        {

            InitializeComponent();

        }

 

        public Form2(IMyInterface myInterface)

        {

            InitializeComponent();

            this.f1 = myInterface;

 

            this.textBox1.ImeMode = ImeMode.Disable;

            this.textBox2.ImeMode = ImeMode.Disable;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            string s1 = textBox1.Text;

            string s2 = textBox2.Text;

 

            int a = int.Parse(s1);

            int b = int.Parse(s2);

            f1.getResult(a, b);

        }

 

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

        {

            //숫자,백스페이스,마이너스,소숫점 만 입력받는다.

            if (!(Char.IsDigit(e.KeyChar)) && e.KeyChar != 8 && e.KeyChar != 45 && e.KeyChar != 46) //8:백스페이스,45:마이너스,46:소수점

            {

                e.Handled = true;

            }

        }

 

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)

        {

            //숫자,백스페이스,마이너스,소숫점 만 입력받는다.

            if (!(Char.IsDigit(e.KeyChar)) && e.KeyChar != 8 && e.KeyChar != 45 && e.KeyChar != 46) //8:백스페이스,45:마이너스,46:소수점

            {

                e.Handled = true;

            }

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            Application.OpenForms["Form2"].Close();

        }

    }

}



 ---

 

2. 델리게이트 이용


namespace FormTransfer

{

    public partial class Form1 : Form

    {

        Form2 f2 = new Form2();

        public Form1()

        {

            InitializeComponent();

            //f2.MyEvent += MyEvent_F1;

            f2.MyEvent += new Form2.MyDelegate(MyEvent_F1);

        }

 

        private void MyEvent_F1(int a, int b)

        {

            int r = a + b;

            label1.Text = r.ToString();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            f2.Show();

        }

    }

}



namespace FormTransfer

{

    public partial class Form2 : Form

    {

        public delegate void MyDelegate(int a, int b);

        public event MyDelegate MyEvent;

 

        public Form2()

        {

            InitializeComponent();

 

            this.textBox1.ImeMode = ImeMode.Disable;

            this.textBox2.ImeMode = ImeMode.Disable;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            string s1 = textBox1.Text;

            string s2 = textBox2.Text;

 

            int a = int.Parse(s1);

            int b = int.Parse(s2);

 

            MyEvent(a, b);

        }

 

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

        {

            //숫자,백스페이스,마이너스,소숫점 만 입력받는다.

            if (!(Char.IsDigit(e.KeyChar)) && e.KeyChar != 8 && e.KeyChar != 45 && e.KeyChar != 46) //8:백스페이스,45:마이너스,46:소수점

            {

                e.Handled = true;

            }

        }

 

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)

        {

            //숫자,백스페이스,마이너스,소숫점 만 입력받는다.

            if (!(Char.IsDigit(e.KeyChar)) && e.KeyChar != 8 && e.KeyChar != 45 && e.KeyChar != 46) //8:백스페이스,45:마이너스,46:소수점

            {

                e.Handled = true;

            }

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            Application.OpenForms["Form2"].Close();

        }

    }

}


반응형