프로그래밍/c# & VB 등
c# Interface를 이용한 부모폼과 자식폼간의 데이터 이동
낼은어떻게
2015. 6. 1. 09:25
부모폼과 자식폼이 있다고 했을때 VB 시절에는 참 쉽게... 다른폼위에 있는 컨트롤이나 메소드를 사용했던것 같은데 c#에서는 복잡하더라구영.. 이런저런 많은 방법이 있지만.
지금 소개하는 interface를 이용하는 방법..... 나름 괜찮은(사실 요부분은 판단할 능력이 없네영. ㅜㅜ) 인듯해서 소개해드립니다.
부모폼에는 텍스박스랑 버튼이랑 라벨이 있고
자식폼엔느 텍스트박스랑 버튼 올려놓으시면 되겠군여
<부모폼 코드>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | namespace p_C_form_connect { public interface IMyInterface { void SetData(String Data); } public partial class Form1 : Form, IMyInterface { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(this as IMyInterface); frm.SetData(label1.Text); frm.Show(); } public void SetData(String Data) { txt_P.Text = Data; } } | cs |
<자식폼 코드>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public partial class Form2 : Form { private IMyInterface frm = null; public Form2(IMyInterface frm) { InitializeComponent(); this.frm = frm; } private void button1_Click(object sender, EventArgs e) { frm.SetData(txt_C.Text); } public void SetData(String Data) { txt_C.Text = Data; } } |
일단 요런식으로 사용하면 부모폼의 라벨1에 있는 텍스트가 자식폼의 텍스트박스로 전송되고
자식폼의 텍스트박스에 입력된 데이터를 부모폼의 텍스트박스로 넘기는 작업까지 수월하게 완료가 되는 것이 확인 할수 있습니다.