2009-07-10 52 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?