2011-07-24 48 views

回答

0

是的,有點反思,你可以做到這一點。在事件處理程序的用戶對象sender獲取父的類型:

Type ChildFormType = ((Button)sender).Parent.GetType(); 

然而,具有使用反射(查詢類型的系統)往往是不好的設計的標誌。某種Visitor Pattern實施方式,其中ParentForm作爲摘要accept方法可能是一種解決方案。

0

我明白,你有4個不同的類爲你的4種形式,所有派生自ParentForm。如果是這樣的話,我將實現ButtonClicked方法是這樣的:

private void button1_Click(object sender, EventArgs e) 
    { 
     // part common to all the forms (possibly void) 
     specific_button1_Click(sender, e); 
     // part common to all the forms (possibly void) 
    } 

    protected void specific_button1_Click(object sender, EventArgs e) 
    { 
    } 

,然後在派生形式