2010-04-28 3 views

답변

3

양식 클래스가 구독 할 수있는 클래스의 이벤트를 노출 할 수 있습니다. 해당 이벤트가 트리거되면 양식은 필요에 따라 UI를 업데이트 할 수 있습니다. 예를 들어 :

class ChildForm : Form 
{ 
    public event EventHandler TextChanged; 

    public string NewText { get { return textBox1.Text; } } 

    void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     EventHandler del = TextChanged; 
     if(del != null) 
     { 
      del(this, e); 
     } 
    } 
} 

class MainForm : Form 
{ 
    void Foo() 
    { 
     using(ChildForm frm = new ChildForm) 
     { 
      frm.TextChanged += (object sender, EventArgs e) => { label1.Text = frm.NewText; }; 
      frm.ShowDialog(); 
     } 
    } 
} 

당신은 실제로 단지이 예에서 바로 아무 통해 TextBox.TextChanged 이벤트를 전달할 수 없었다.

+0

제 경우 ChildForm은 양식이 아닙니다. 이런 식으로해도 될까요? –

+0

예, 그 예입니다. 개념은 같습니다. –

관련 문제