Solved

Letting a click fall through

Posted on 2006-11-28
2
288 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: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
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:Tom 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

737 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