Wrox Programmer Forums
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 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 February 23rd, 2010, 04:17 PM
Authorized User
 
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help in custom Class

Help in custom Class

Hi Everybody
I made a simple project its idea as follow

i want to make a graphical object consist of filled rectangle which can be drawn on the screen and be able to move it any where on the screen. i created windows form and added

to it a new class which represents my graphical object and contains the following code
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace DrawObject
{
   public class MyObject
    {
        Point postion;
        Size size;
        Region objRegion;

       public MyObject()
        {
            postion = new Point(20, 30);
            size = new Size(30, 30);
            objRegion = new Region(new Rectangle(postion, size));

        }
        public void Draw(Graphics g)
            {
                g.FillRectangle(Brushes.Blue, postion.X, postion.Y, size.Width, size.Height);
                g.Dispose();
            }
        public Point Postion
            {
                get { return postion; }
                set { 
                        postion = value;
                        objRegion.Dispose();
                        objRegion = new Region(new Rectangle(postion, size));
                    }
            }
        public Size SizeObj
            {
                get { return size; }
                set { 
                        size = value;
                        objRegion.Dispose();
                        objRegion = new Region(new Rectangle(postion, size));
                    }
            }
    }
}
my question is how to make my class detect mouse click so that i can register any fn to mouse click event (i mean when i click on my graphic object)

Last edited by falcon eyes; February 24th, 2010 at 04:42 AM..
 
Old February 23rd, 2010, 05:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Code:
public Form1()
{
   InitializeComponent();
   MyObject myObject = new MyObject();
   this.MouseClick += new MouseEventHandler(myObject.Handle_MouseClick);
}
Code:
public class MyObject
{
 public void Handle_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) 
 {
    // Do something.
 }
}
 
Old February 24th, 2010, 04:38 AM
Authorized User
 
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx Bob Bedell
but your code make the whole form to respond to the mouse click not only my graphic object since you register a fn with the form mouse click event and i want only my object to respond to the mouse click on him
 
Old February 24th, 2010, 04:53 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you want you class to act like a user control (accepting mouse clicks etc) then you'll have to inherit from UserControl, or similar, and override the OnClick and OnPaint methods.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 24th, 2010, 04:31 PM
Authorized User
 
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i will try your suggestion samjudson but is there any other method like using message filter
 
Old February 24th, 2010, 05:32 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm not sure what you mean by message filters. Sounds like you are trying to make life harder for yourself. UserControl already has all the properties you need, Size, Location and BackColor.

Code:
public class MyObject : UserControl
{
  protected override OnClick(EventArgs e)
  {
    // Do what ever you want here

    base.OnClick(e);
  }
}
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 25th, 2010, 05:53 AM
Authorized User
 
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes you are right,but i was trying also to learn about message filter class if someone know
 
Old February 25th, 2010, 06:19 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

What message filter class?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 26th, 2010, 07:25 AM
Authorized User
 
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
What message filter class?
sorry it's not a class
It's an interface used to catch any event before it's redirected to the application and then do any action required and to use it you must create a class that built this interface

Last edited by falcon eyes; February 26th, 2010 at 07:33 AM..
 
Old February 26th, 2010, 11:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Yes, but unless I'm mistake C# and .Net don't work like that, at least not with Winforms.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom style class oldSkool BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 0 January 9th, 2007 03:39 AM
Custom Default Class Template Bob Bedell C# 3 November 11th, 2006 03:18 PM
Ajax and Custom Class ~Bean~ ASP.NET 1.0 and 1.1 Professional 0 April 6th, 2006 12:33 PM
DataRow Vs Custom Class RM82 BOOK: ASP.NET Website Programming Problem-Design-Solution 2 April 3rd, 2004 06:01 AM





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