Tom Knowlton
asked on
Letting a click fall through
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?
How can I let a MouseDown event on either PictureBox fall through to the UserControl, such that the UserControl can respond to the MouseDown?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Pictu
private System.Windows.Forms.Pictu
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
private int startX, startY;
private int startGPX, startGPY;
private WindowsApplication2.GamePi
private WindowsApplication2.GamePi
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.Pictu
this.pictureBox2 = new System.Windows.Forms.Pictu
this.gamePiece1 = new WindowsApplication2.GamePi
this.gamePiece2 = new WindowsApplication2.GamePi
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BorderSty
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
this.pictureBox2.BorderSty
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.Mouse
this.pictureBox2.MouseMove
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.Mouse
this.gamePiece2.MouseMove += new System.Windows.Forms.Mouse
this.gamePiece2.MouseDown += new System.Windows.Forms.Mouse
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 334);
this.Controls.Add(this.gam
this.Controls.Add(this.gam
this.Controls.Add(this.pic
this.Controls.Add(this.pic
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.F
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(obje
{
if (e.Button == MouseButtons.Left)
{
pictureBox2.Location = new Point(pictureBox2.Left + e.X - startX, pictureBox2.Top + e.Y - startY);
}
}
private void pictureBox2_MouseDown(obje
{
if (e.Button == MouseButtons.Left)
{
startX = e.X;
startY = e.Y;
}
}
private void pictureBox2_MouseUp(object
{
if (e.Button == MouseButtons.Left)
{
Rectangle pb1 = new Rectangle(pictureBox1.Loca
Rectangle pb2 = new Rectangle(pictureBox2.Loca
if (pb1.IntersectsWith(pb2))
{
//MessageBox.Show("Overlap
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.Mouse
{
if (e.Button == MouseButtons.Left)
{
Rectangle pb1 = new Rectangle(this.gamePiece1.
Rectangle pb2 = new Rectangle(this.gamePiece2.
if (pb1.IntersectsWith(pb2))
{
//MessageBox.Show("Overlap
this.gamePiece2.Location = this.gamePiece1.Location;
}
else
{
//MessageBox.Show("NOT Overlapping");
}
}
}
private void gamePiece2_MouseDown(objec
{
if (e.Button == MouseButtons.Left)
{
startGPX = e.X;
startGPY = e.Y;
}
}
private void gamePiece2_MouseMove(objec
{
if (e.Button == MouseButtons.Left)
{
this.gamePiece2.Location = new Point(this.gamePiece2.Left
}
}
private void gamePiece2_MouseUp_1(objec
{
if (e.Button == MouseButtons.Left)
{
Rectangle pb1 = new Rectangle(this.gamePiece1.
Rectangle pb2 = new Rectangle(this.gamePiece2.
if (pb1.IntersectsWith(pb2))
{
//MessageBox.Show("Overlap
this.gamePiece2.Location = this.gamePiece1.Location;
}
else
{
//MessageBox.Show("NOT Overlapping");
}
}
}
private void gamePiece2_MouseMove_1(obj
{
if (e.Button == MouseButtons.Left)
{
this.gamePiece2.Location = new Point(this.gamePiece2.Left
}
}
private void gamePiece2_MouseDown_1(obj
{
if (e.Button == MouseButtons.Left)
{
this.gamePiece2.Location = new Point(this.gamePiece2.Left
}
}
}
}
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.UserC
{
private System.Windows.Forms.Pictu
private System.Windows.Forms.Pictu
private System.Windows.Forms.ToolT
private System.ComponentModel.ICon
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.Cont
this.pictureBoxImage = new System.Windows.Forms.Pictu
this.pictureBoxHealth = new System.Windows.Forms.Pictu
this.toolTipInfo = new System.Windows.Forms.ToolT
this.SuspendLayout();
//
// pictureBoxImage
//
this.pictureBoxImage.Borde
this.pictureBoxImage.Locat
this.pictureBoxImage.Name = "pictureBoxImage";
this.pictureBoxImage.Size = new System.Drawing.Size(32, 32);
this.pictureBoxImage.TabIn
this.pictureBoxImage.TabSt
this.pictureBoxImage.Mouse
//
// pictureBoxHealth
//
this.pictureBoxHealth.Bord
this.pictureBoxHealth.Loca
this.pictureBoxHealth.Name
this.pictureBoxHealth.Size
this.pictureBoxHealth.TabI
this.pictureBoxHealth.TabS
//
// GamePiece
//
this.Controls.Add(this.pic
this.Controls.Add(this.pic
this.Name = "GamePiece";
this.Size = new System.Drawing.Size(40, 40);
this.Load += new System.EventHandler(this.G
this.ResumeLayout(false);
}
#endregion
private void GamePiece_Load(object sender, System.EventArgs e)
{
this.toolTipInfo.SetToolTi
this.toolTipInfo.SetToolTi
}
private void pictureBoxImage_MouseHover
{
//this.toolTipInfo.
}
}
}