Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 15th, 2006, 09:32 PM
Authorized User
Join Date: Sep 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to guntank
Default Forward Mouse event in Main window to its controls

I have a problem about how to forward mouse events from my main window to its controls.

I create a user control called Sprite control. It must be able to move whenever it is dragged.
So the Sprite control will handle MouseDown, MouseMove and MouseUp events as follows

========= begin Sprite control =========

        Boolean isDrag = false;
        private void Sprite_MouseDown(object sender, MouseEventArgs e)
            this.Left = this.Left + e.X - this.Width / 2;
            this.Top = this.Top + e.Y - this.Height / 2;
            isDrag = true;

        private void Sprite_MouseMove(object sender, MouseEventArgs e)
            if (isDrag)

                this.Left = this.Left + e.X - this.Width / 2 ;
                this.Top = this.Top + e.Y - this.Height / 2;

        private void Sprite_MouseUp(object sender, MouseEventArgs e)
            isDrag = false;

        private void Sprite_Paint(object sender, PaintEventArgs e)
            e.Graphics.DrawRectangle(new Pen(Brushes.Blue, 2), ClientRectangle);

========= end Sprite control =========

I add the Sprite control into my main window, it is set to invisible at design time. It will be visible if the users click and drag everywhere in the main window. After releasing the mouse, it becomes invisible again.

In my main window, as a consumer of the Sprite control, the code is as follows

========= begin main window =========

        private void Form1_MouseUp(object sender, MouseEventArgs e)
            sprite1.Visible = false;

        private void Form1_MouseDown(object sender, MouseEventArgs e)
            sprite1.Location = new Point(e.X - sprite1.Width / 2, e.Y - sprite1.Height / 2);
            sprite1.Visible = true;

========= end main window =========

My problem is that the Sprite's mouse event handlers never get executed when users are doing click and drag on the main window (but before releasing the mouse).

So I need to forward the mouse event from the main window to the Sprite control here.
How to do this as easy as possible without using hook mechanism or any complicated ways.

I want to make the Sprite control as compact as possible for the consumer (Main window in this case) so its position must be handled by its own mouse events rather than by the consumer.

Thank you in advance.

Andi Setiawan

Similar Threads
Thread Thread Starter Forum Replies Last Post
right click mouse event sen22 ASP.NET 2.0 Basics 5 December 16th, 2006 08:46 AM
Is there an Event for resizing the main XL window? labdad Excel VBA 2 January 6th, 2006 07:23 PM
Mouse Event Question muklee Javascript How-To 1 June 9th, 2005 07:44 AM
Mouse Wheel Event BSkelding ASP.NET 1.0 and 1.1 Professional 3 March 30th, 2005 09:12 AM
Mouse Event muklee Javascript How-To 6 October 1st, 2004 10:10 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.