Advertisement

05.06.2008 at 05:00PM PDT, ID: 23381294
[x]
Attachment Details

Raising events on windows forms

Asked by korhanacar in Microsoft Visual C#.Net

Tags: Microsoft, C#, .NET 3.5, C#, Raising events on windows forms

Well the thing is I'm in the middle of learning how to raise my own events but I got stuck.

I already understand the delegates and the events, and I'm trying to create an object with an "OnMouseHover" event, what I'm doing is I'm drawing a square on the Form and after that would come that if the mouse is hovering on it (depending on it's drawing point) it would raise the OnMouseHover event, but I don't know where to do that. I mean there must be something continiouslly going on so it can raise the event as soon as the mouse enters the square. Anyways, the code is bellow. I know there's something missing to raise the CheckMouseHover method when the mouse does hover the square, but I don't have the slightest idea of what it is.

Thanks for your help.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
this is the form
 
public partial class Form1 : Form
    {
        MySquare s = new MySquare(new Point(50, 50), Color.Red, new Size(50, 50));
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            s.OnMouseHover += new MouseMoveEventHandler(s_OnMouseHover);
        }
 
        void s_OnMouseHover(object o, MouseMoveEventArgs e)
        {
            MessageBox.Show("Mouse hovering");   
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            s.Draw(e);
        }
    }
 
this is the class
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
 
namespace CustomStuff
{
    delegate void MouseMoveEventHandler(object o, MouseMoveEventArgs e);
 
    class MySquare
    {
        private Point _point;
        private Color _color;
        private Size _size;
 
        public event MouseMoveEventHandler OnMouseHover;
 
        public MySquare(Point point, Color color, Size size)
        {
            this._point = point;
            this._color = color;
            this._size = size;
        }
 
        public void Draw(PaintEventArgs e)
        {
            Rectangle square = new Rectangle(this._point, this._size);
            Form1 form = new Form1();
 
            Graphics g = e.Graphics;
 
            g.FillRectangle(Brushes.Red, square);
 
            Pen p = new Pen(this._color);
            
            g.DrawRectangle(p, square);
            p.Dispose();
        }
 
        private void CheckMouseHoverEvent(object o, MouseMoveEventArgs e)
        {
            if (OnMouseHover != null)
            {
                OnMouseHover(o, e);
            }
        }
    }
 
    class MouseMoveEventArgs : EventArgs
    {
        private readonly int posX;
        private readonly int posY;
 
        public MouseMoveEventArgs(int posX, int posY)
        {
            this.posX = posX;
            this.posY = posY;
        }
    }
}
 
Loading Advertisement...
 
[+][-]05.06.2008 at 06:01PM PDT, ID: 21512379

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.07.2008 at 09:11AM PDT, ID: 21517869

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.08.2008 at 02:38PM PDT, ID: 21528698

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual C#.Net
Tags: Microsoft, C#, .NET 3.5, C#, Raising events on windows forms
Sign Up Now!
Solution Provided By: TheLearnedOne
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.08.2008 at 02:40PM PDT, ID: 21528711

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628