Solved

Letting a click fall through

Posted on 2006-11-28
2
282 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exception in Log4Net 1 20
C# Application Local DB Connection String 23 62
Data Saving 5 40
fomat Json objects 6 20
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!
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

15 Experts available now in Live!

Get 1:1 Help Now