views:

203

answers:

2

Hello everyone

I have added an EventHandler for the Click-event to a picturebox but on runtime this handler is never called (the debugger shows me that it is added to the control directly but when I click on the picturebox nothing happens).

I assume it has something to do with my inheritance. I have a usercontrol called AbstractPage (its not really abstract since the designer doesnt like that) which only consists of a heading and this picturebox but it provides quite some functions the actual pages rely on.

#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

The page implementations just inherit this class and add their controls and behavior. We recently figured out that subclassing UserControl is not performant and we lose some performance there, but its the best way to do it (I dont want to c&p function for 25 pages and maintain them).

My pageA looks like this

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)
    {
    ...
    }

Anyway, the *picLock_Click* is never called and I dont know why?

The pages are all put into a PageControl which consists of a TreeView and a TabContainer where the pages are put once I call addPage(IPage)

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 
}

Thanks in advance

+1  A: 

If I understand your problem correctly, this worked for me out of the box (using VS2k8). My code:

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();
    }
}

I moved the TestUserControl to a form, clicked the button and got the message box as expected. Can you paste some more code, e.g. how do you use your AbstractPage?

Grzenio
strange that it worked right away...but thats what I would expect anyway :)I added some more code to my question. I played around but it never worked.
lostiniceland
+1  A: 

I found the problem. We are using the Infragistics WinForms but in that case I used the standard picturebox. I replaced it with the UltraPictureBox and now it works.

lostiniceland