2009-07-10 59 views
2

我已經爲Clickbox事件添加了一個EventHandler給一個picturebox,但是在運行時這個處理程序永遠不會被調用(調試器向我顯示它直接添加到控件中,但是當我單擊在picturebox上沒有任何反應)。如何在父類中調用EventHandler

我認爲這與我的繼承有關。我有一個名爲AbstractPage的用戶控件(它不是真正的抽象,因爲設計者不喜歡這樣),它只包含一個標題和這個圖片框,但它提供了一些實際頁面依賴的功能。

#region Constructor 
public AbstractPage() 
{ 
    InitializeComponent(); 
    lblHeading.Text = PageName; 
    picLock.Click += new EventHandler(picLock_Click); 
} 
#endregion 

#region Events 
void picLock_Click(object sender, EventArgs e) 
{ 
    ...do some stuff 
} 
#endregion 

頁面實現只是繼承這個類並添加它們的控件和行爲。我們最近發現UserControl的子類不是高性能的,我們在那裏失去了一些性能,但是它是最好的方法(我不想爲25個頁面提供p函數並維護它們)。

我pageA的看起來像這樣

public partial class PageA : AbstractPage 
{ 
    #region Constructor 
    public PageA() 
    { 
    // I dont call the base explicitely since it is the 
    // standard constructor and this always calls the base 
     InitializeComponent(); 
    } 
    #endregion 

    public override string PageName 
    { 
     get { return "A"; } 
    } 

    public override void BindData(BindingSource dataToBind) 
    { 
    ... 
    } 

反正picLock_Click不會被調用,我不知道爲什麼?

頁都投入其中包括一個TreeView和其中的頁面放置有一次我打電話addPage(的iPage)一個TabContainer的的的PageControl

public partial class PageControl { 
    ... 
protected virtual void AddPages() 
{ 
    AddPage(new PageA());  
    AddPage(new PageD()); 
    AddPage(new PageC()); 
    ... 
} 

protected void AddPage(IPage page) 
{ 
    put pagename to treeview and enable selection handling 
    add page to the tabcontainer  
} 

在此先感謝

+0

我有同樣的問題,我從來沒有解決。我通過在每個派生類中實現處理程序並調用基本方法來解決此問題 – Calanus 2009-07-10 10:57:22

+1

設計人員不喜歡抽象?從什麼時候抽象一個味道的案例? – Dykam 2009-07-10 11:02:51

回答

1

我發現了這個問題。我們正在使用Infragistics WinForms,但在這種情況下,我使用了標準的picturebox。我用UltraPictureBox替換它,現在它工作。

1

如果我理解你的問題正確,這爲我開箱即用(使用VS2k8)。我的代碼:

public partial class BaseUserControl : UserControl 
{ 
    public BaseUserControl() 
    { 
     InitializeComponent(); //event hooked here 
    } 

    private void showMsgBox_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("Button clicked"); 
    } 
} 

public partial class TestUserControl : BaseUserControl 
{ 
    public TestUserControl() 
    { 
     InitializeComponent(); 
    } 
} 

我將TestUserControl移動到窗體上,單擊按鈕並按預期得到消息框。你能否粘貼更多的代碼,例如你如何使用你的AbstractPage?