Solved

Let a click "fall through"

Posted on 2007-12-02
10
922 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:knowlton
  • 5
  • 4
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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:knowlton
Comment Utility
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
Comment Utility
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
 
LVL 5

Author Comment

by:knowlton
Comment Utility
can you please paste in code that shows how this works with a derived picturebox?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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:knowlton
Comment Utility
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
Comment Utility
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:knowlton
Comment Utility
I still don't see it in the Toolbox, sorry.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now