Solved

Letting a click fall through

Posted on 2006-11-28
2
284 Views
Last Modified: 2010-04-16
I have a UserControl with a PictureBox control on it (Docking set to FILL) and then on that PictureBox I have another PictureBox.

How can I let a MouseDown event on either PictureBox fall through to the UserControl, such that the UserControl can respond to the MouseDown?
0
Comment
Question by:knowlton
2 Comments
 
LVL 8

Accepted Solution

by:
sabeesh earned 500 total points
ID: 18034293
YOu have to write a delegate to trap the mouse down event
0
 
LVL 5

Author Comment

by:knowlton
ID: 18050404
Can you give me an example?

Here is my MainForm source:


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.PictureBox pictureBox1;
            private System.Windows.Forms.PictureBox pictureBox2;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            private int startX, startY;
            private int startGPX, startGPY;
            private WindowsApplication2.GamePiece gamePiece1;
            private WindowsApplication2.GamePiece gamePiece2;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.pictureBox1 = new System.Windows.Forms.PictureBox();
                  this.pictureBox2 = new System.Windows.Forms.PictureBox();
                  this.gamePiece1 = new WindowsApplication2.GamePiece();
                  this.gamePiece2 = new WindowsApplication2.GamePiece();
                  this.SuspendLayout();
                  //
                  // pictureBox1
                  //
                  this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                  this.pictureBox1.Location = new System.Drawing.Point(104, 80);
                  this.pictureBox1.Name = "pictureBox1";
                  this.pictureBox1.TabIndex = 0;
                  this.pictureBox1.TabStop = false;
                  //
                  // pictureBox2
                  //
                  this.pictureBox2.BackColor = System.Drawing.SystemColors.ActiveCaption;
                  this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                  this.pictureBox2.Location = new System.Drawing.Point(328, 216);
                  this.pictureBox2.Name = "pictureBox2";
                  this.pictureBox2.TabIndex = 1;
                  this.pictureBox2.TabStop = false;
                  this.pictureBox2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseUp);
                  this.pictureBox2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseMove);
                  this.pictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDown);
                  //
                  // gamePiece1
                  //
                  this.gamePiece1.Location = new System.Drawing.Point(320, 128);
                  this.gamePiece1.Name = "gamePiece1";
                  this.gamePiece1.Size = new System.Drawing.Size(40, 40);
                  this.gamePiece1.TabIndex = 5;
                  //
                  // gamePiece2
                  //
                  this.gamePiece2.Location = new System.Drawing.Point(144, 184);
                  this.gamePiece2.Name = "gamePiece2";
                  this.gamePiece2.Size = new System.Drawing.Size(40, 40);
                  this.gamePiece2.TabIndex = 6;
                  this.gamePiece2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.gamePiece2_MouseUp_1);
                  this.gamePiece2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.gamePiece2_MouseMove_1);
                  this.gamePiece2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.gamePiece2_MouseDown_1);
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(504, 334);
                  this.Controls.Add(this.gamePiece2);
                  this.Controls.Add(this.gamePiece1);
                  this.Controls.Add(this.pictureBox2);
                  this.Controls.Add(this.pictureBox1);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.Load += new System.EventHandler(this.Form1_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void pictureBox2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        pictureBox2.Location = new Point(pictureBox2.Left + e.X - startX, pictureBox2.Top + e.Y - startY);
                  }

            }

            private void pictureBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        startX = e.X;
                        startY = e.Y;
                  }

            }

            private void pictureBox2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        Rectangle pb1 = new Rectangle(pictureBox1.Location, pictureBox1.Size);
                        Rectangle pb2 = new Rectangle(pictureBox2.Location, pictureBox2.Size);
                        if (pb1.IntersectsWith(pb2))
                        {
                              //MessageBox.Show("Overlapping");
                              pictureBox2.Location = pictureBox1.Location;
                        }
                        else
                        {
                              //MessageBox.Show("NOT Overlapping");
                        }
                  }

            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
            
            }

            private void gamePiece2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        Rectangle pb1 = new Rectangle(this.gamePiece1.Location, this.gamePiece1.Size);
                        Rectangle pb2 = new Rectangle(this.gamePiece2.Location, this.gamePiece2.Size);
                        if (pb1.IntersectsWith(pb2))
                        {
                              //MessageBox.Show("Overlapping");
                              this.gamePiece2.Location = this.gamePiece1.Location;
                        }
                        else
                        {
                              //MessageBox.Show("NOT Overlapping");
                        }
                  }            
            }

            private void gamePiece2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        startGPX = e.X;
                        startGPY = e.Y;
                  }
            }

            private void gamePiece2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        this.gamePiece2.Location = new Point(this.gamePiece2.Left + e.X - startGPX, this.gamePiece2.Top + e.Y - startGPY);
                  }
            }

            private void gamePiece2_MouseUp_1(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        Rectangle pb1 = new Rectangle(this.gamePiece1.Location, this.gamePiece1.Size);
                        Rectangle pb2 = new Rectangle(this.gamePiece2.Location, this.gamePiece2.Size);
                        if (pb1.IntersectsWith(pb2))
                        {
                              //MessageBox.Show("Overlapping");
                              this.gamePiece2.Location = this.gamePiece1.Location;
                        }
                        else
                        {
                              //MessageBox.Show("NOT Overlapping");
                        }
                  }
            }

            private void gamePiece2_MouseMove_1(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        this.gamePiece2.Location = new Point(this.gamePiece2.Left + e.X - startGPX, this.gamePiece2.Top + e.Y - startGPY);
                  }
            }

            private void gamePiece2_MouseDown_1(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        this.gamePiece2.Location = new Point(this.gamePiece2.Left + e.X - startGPX, this.gamePiece2.Top + e.Y - startGPY);
                  }
            }

            
      }
}




Here is my UserControl source:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WindowsApplication2
{
      /// <summary>
      /// Summary description for GamePiece.
      /// </summary>
      public class GamePiece : System.Windows.Forms.UserControl
      {
            private System.Windows.Forms.PictureBox pictureBoxImage;
            private System.Windows.Forms.PictureBox pictureBoxHealth;
            private System.Windows.Forms.ToolTip toolTipInfo;
            private System.ComponentModel.IContainer components;

            public GamePiece()
            {
                  // This call is required by the Windows.Forms Form Designer.
                  InitializeComponent();

                  // TODO: Add any initialization after the InitializeComponent call

            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if(components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Component Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.components = new System.ComponentModel.Container();
                  this.pictureBoxImage = new System.Windows.Forms.PictureBox();
                  this.pictureBoxHealth = new System.Windows.Forms.PictureBox();
                  this.toolTipInfo = new System.Windows.Forms.ToolTip(this.components);
                  this.SuspendLayout();
                  //
                  // pictureBoxImage
                  //
                  this.pictureBoxImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                  this.pictureBoxImage.Location = new System.Drawing.Point(0, 0);
                  this.pictureBoxImage.Name = "pictureBoxImage";
                  this.pictureBoxImage.Size = new System.Drawing.Size(32, 32);
                  this.pictureBoxImage.TabIndex = 0;
                  this.pictureBoxImage.TabStop = false;
                  this.pictureBoxImage.MouseHover += new System.EventHandler(this.pictureBoxImage_MouseHover);
                  //
                  // pictureBoxHealth
                  //
                  this.pictureBoxHealth.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                  this.pictureBoxHealth.Location = new System.Drawing.Point(4, 24);
                  this.pictureBoxHealth.Name = "pictureBoxHealth";
                  this.pictureBoxHealth.Size = new System.Drawing.Size(20, 5);
                  this.pictureBoxHealth.TabIndex = 1;
                  this.pictureBoxHealth.TabStop = false;
                  //
                  // GamePiece
                  //
                  this.Controls.Add(this.pictureBoxHealth);
                  this.Controls.Add(this.pictureBoxImage);
                  this.Name = "GamePiece";
                  this.Size = new System.Drawing.Size(40, 40);
                  this.Load += new System.EventHandler(this.GamePiece_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            private void GamePiece_Load(object sender, System.EventArgs e)
            {
                  this.toolTipInfo.SetToolTip(this.pictureBoxHealth,"Health");
                  this.toolTipInfo.SetToolTip(this.pictureBoxImage,"My name is Tom");
            }

            private void pictureBoxImage_MouseHover(object sender, System.EventArgs e)
            {
                  //this.toolTipInfo.
            }
      }
}
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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