Solved

Let a click "fall through"

Posted on 2007-12-02
10
951 Views
Last Modified: 2012-06-21
I have a PictureBox (MAIN), upon which I am drawing a bunch of smaller PictureBoxes (and assigning as their parent, the larger PictureBox), such that they completely overlap the parent PictureBox.

The problem I have run into now is I have signed-up for the OnClick for the MAIN PictureBox -- but it is not being processed because the click is happening on one of the child PictureBoxes and is never "reaching" the Parent (MAIN) PictureBox.  How can I let the click "fall through" and get processed by the Parent?
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20391810
on the child's picturebox event, you can retrasmit the event manually. Derive your own picturebox class




public class ChildPictureBox : PictureBox
{
      protected override void OnClick(EventArgs e)
      {
            if (this.Parent != null)
                 Parent.OnClick(e);
            base.OnClick(e);
      }
}

Open in new window

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 20392826
And what coordinates will be passed through to the Parent?  Will it be the relative coordinates of the child, or the coordinates of the Parent?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392931
hmm, them will be relative to the child picture box, but you can convert with an ugly trick:

notice the onclick method doesn't pass the mouse cursor coordinates. Use the OnMouseClick instead, with some conversion tricks:
 
protected virtual void OnMouseClick (MouseEventArgs e)
{
     base.OnMouseClick(e);
     if (this.Parent != null)
     {
           e.Location = Parent.PointToClient(this.PointToScreen(e.Location));
           Parent.MouseOnClick(e);
     }
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 20451509
can you please paste in code that shows how this works with a derived picturebox?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20452088
create your derived class as shown above. The control will appear on the toolbox when designing your form, use it instead of PictureBox.
In your form, handle the Click event, it will be fired every time the picture box is clicked.


public class MappedPictureBox : PictureBox
{
    protected virtual void OnMouseClick (MouseEventArgs e)
    {
       base.OnMouseClick(e);
       if (this.Parent != null)
       {
           e.Location = Parent.PointToClient(this.PointToScreen(e.Location));
           Parent.MouseOnClick(e);
       }
    }
}

Open in new window

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 20452185
which kind of "new item" do I pick from "Add New Item"?

That is part of my confusion is - how / where do I create this derived control such that it will appear on the Toolbox?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20452258
if you create the class and include it in your project, just like other .cs file. The control will automatically appear in the toolbox.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 20452304
I still don't see it in the Toolbox, sorry.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20454371
Hmm, there are many things to consider. First you have to compile to allow the control appear in your toolbox. But the code won't compile as is, you will need to derive  the main picture box to allow the code to work, but you can reuse it this way:

namespace System.Windows.Forms
{
    public class MappedPictureBox : PictureBox
    {
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (this.Parent != null && this.Parent is MappedPictureBox)
            {
                Point p = Parent.PointToClient(this.PointToScreen(e.Location));
                MouseEventArgs mea = new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta);
                ((MappedPictureBox)Parent).OnMouseClick(mea);  // perform the click
            }
        }
    }
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Header Font Size in Grid View 6 51
Convert VB web project to C# 3 67
Run software updates from the website 6 51
C# Change the instruction to user. 11 39
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question