Hello,

I have a Form and a Class which extends Panel and inside this class, I have declared a PictureBox.

I need the MyPanel class to return the MouseEventHandler of the PictureBox, not the Panel.
The PictureBox is the same size of the Panel and therefore I cannot click the Panel underneath.

I am currently adding the MouseEvent Handler directly and keeping the PictureBox public i.e.

class MyPanel : Panel
{
   public PictureBox MyImage;

   public MyPanel()
   {
    MyImage = new PictureBox();
   }
}

class MainForm : Form
{
   public MyForm()
   {
    testpanel.MyImage.MouseDown += new MouseEventHandler(PictureBox_MouseDown);
   }

   public void PictureBox_MouseDown(object sender, MouseEventArgs m)
   {
       // Do Stuff
   }
}

but I want to keep the PictureBox private and encapsulated.

How Do I do this.

Be careful when you create inherit classes -- your constructor is not formed properly. You should call : base() .

Also if you fire the click event like that it will expose the picture box as the sender. Also this adds a new event and doesn't change the existing panels MouseDown event. It wasn't clear how you wanted to go about this.

using System.Windows.Forms;

namespace daniweb
{
  public sealed class MyPanel : Panel
  {
    public event MouseEventHandler MouseDownPicture;
    private PictureBox myImage;
    public MyPanel()
      : base()
    {
      myImage = new PictureBox();
      this.Controls.Add(myImage);
      myImage.Dock = DockStyle.Fill;
      myImage.MouseDown += new MouseEventHandler(myImage_MouseDown);
    }
    void myImage_MouseDown(object sender, MouseEventArgs e)
    {
      if (this.MouseDownPicture != null)
      {
        this.MouseDownPicture(sender, e);
      }
    }
  }
}

Calling it:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmPictureBox : Form
  {
    public frmPictureBox()
    {
      InitializeComponent();
      this.panel1.MouseDownPicture += new MouseEventHandler(panel1_MouseDownPicture);
    }

    void panel1_MouseDownPicture(object sender, MouseEventArgs e)
    {
      if (sender is PictureBox)
        MessageBox.Show("You clicked on the picture box... but this also exposes the member you're trying to encapsulate");
      System.Diagnostics.Debugger.Break();
    }
  }
}

You could hide the pictureBox as the sender by changing the firing mechanism. This will fire it with the panel:

void myImage_MouseDown(object sender, MouseEventArgs e)
    {
      if (this.MouseDownPicture != null)
      {
        this.MouseDownPicture(this, e); //using this instead of sender
      }
    }

Excellent, Thats just what I needed.

Please mark this thread as solved as you have found a solution to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.