일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 26 | 27 | 28 | 29 | 30 | 31 |
- 원한
- STS
- 명령어
- checkbox
- SCADA
- JQuery
- Custom
- Linux
- 리눅스
- Python
- 맥코트
- ORA-28002
- 분노
- pythoncom37.dll
- error
- build
- 파이썬
- Eclipse
- DataTables
- 말라키
- Anaconda
- geckodriver
- 가상환경
- HMI
- LOG
- Today
- Total
2010년 5월 1일, 2막
[C#] 폼간 데이터 전송 본문
출처:Form간 데이터 전달 시, 상황에 맞는 응용 방법
참고 사이트 |
1. 자식폼 종료 시, 부모폼으로 데이터 전송
// main form to read data set in child form / dialog public partial class MainForm : Form { public MainForm() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { DraftForm form = new DraftForm();
if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) { textBox1.Text = form.DraftNumber; } } }
// another form where data is read from public partial class DraftForm : Form { public DraftForm() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { DialogResult = System.Windows.Forms.DialogResult.OK; }
public string DraftNumber { get { return this.textBox1.Text; } } } |
2. 자식폼만 활성화 된 후, 부모폼으로 여러 데이터를 전송할 때
[자식폼] // delegate 이벤트선언 public delegate void FormSendDataHandler(object obj); public event FormSendDataHandler FormSendEvent;
// 창을 닫을 때 FormSendEvent 이벤트를 실행한다. 파라미터로 object 를 하나 넘긴다. private void btnFormClose_Click(object sender, EventArgs e) { int no = cboDisSystem.SelectedIndex; this.FormSendEvent(disSystemNo[no]); this.Dispose(); } [부모폼] // 자식폼을 실행할 때 자식폼에 설정되어있는 이벤트에 DieaseUpdateEventMethod // 실행할 메소드명을 등록한다. 자식폼에서 이벤트를 실행하면 이 메소드가 실행될것이다. private void btnReasonAdd_Click(object sender, EventArgs e) { FrmAdd frm = new FrmAdd (); frm.FormSendEvent += new FrmAdd.FormSendDataHandler(DieaseUpdateEventMethod); frm.ShowDialog(); }
private void DieaseUpdateEventMethod(object sender) { Console.WriteLine("이벤트 실행"); } |
3. 많은 폼이 활성화된 상태일 때, 상호 유기적인 데이터 교환을 원할 때(Interface를 사용하여 데이터를 가져올 때 - OOP에 가장 맞는 방법이라 생각됨. Interface 자체가 이런거 하려고 만들어진 거라서.)
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.Show(); }
public void SetData(String Data) { textBox1.Text = Data; } }
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(textBox1.Text); } }
|
'Computer > Tips' 카테고리의 다른 글
[C#] 크로스 스레드와 Control.Invoke (0) | 2019.07.24 |
---|---|
[DB] EXPLAIN for MariaDB (0) | 2019.07.23 |
[C#] OpenXML error : The type 'Package' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxx' error (0) | 2019.06.19 |
[c#] delegate, event (0) | 2019.06.14 |
[C#] BinaryReader 사용 시 한글깨짐 처리 (0) | 2019.05.26 |