[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Detecting events generated by a custom control

Asked by yaronusa in C# Programming Language, Microsoft Visual C#.Net, .Net Editors & IDEs

Tags: c# csharp c sharp .net

I have a form that has in it a custom control I created.

I want my Form (that holds that custom control) to know when a mouse click or mouse over event has occurred over that custom control.

The important thing for me to be able to do is to know the xy coordinate of where the click on the control took place.

The code snippet shows my control... thanks for all your help.

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:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
 
namespace gridCustomControl
{
    public partial class GridCustomControl : UserControl
    {
        private int _cols = 3;
        private int _rows = 3;
        private int _width = 10;     // Cell width
        private int _height = 10;    // Cell height
        private float _zoom = 2f;
        private Color _cellColor = Color.White;
        private Color _borderColor = Color.Black;
        private const int WM_HSCROLL = 0x114;
        private const int WM_VSCROLL = 0x115;
 
        public GridCustomControl()
        {
            // We do not want to call InitializeComponent() from the
            // Designer of this custom control because we want the user
            // to be able to set the size of the control at design time.
            InitializeComponent();
 
            // The following SetStyles are needed for double buffering:
            this.SetStyle(ControlStyles.DoubleBuffer | 
                          ControlStyles.AllPaintingInWmPaint | 
                          ControlStyles.UserPaint, true);
 
            this.SetControlSize();
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.FillRectangle(Brushes.White, this.ClientRectangle);
 
            int cellHeight = this.CellHeight;
            int cellWidth = this.CellWidth;
            int nbrColumns = this.Columns;
            int nbrRows = this.Rows;
            int zoom = Convert.ToInt32(this.Zoom);
            Size scrollOffset;
 
            
 
            // Draw cells
            for (int row = 0; row < nbrRows; row++)
            {
                for (int col = 0; col < nbrColumns; col++)
                {
                    // Get negative x & y coordinates of scroll
                    scrollOffset = new Size(this.AutoScrollPosition);
 
                    // Calculate new cell location
                    // Detailed Explanation of example of offsets:
                    /* Assume the first cell top-left corner is located at x:0 y:0, 
                     * or (0,0). If there aren't any scrolling offsets, and the cell
                     * at (0,0) intersects with the ClipRectangle, then it will need
                     * to be painted. If there was scrolling of x:-100 (width) and
                     * y:-100 (height) then the cell at (-100, -100) will not be drawn
                     * because it is not within the 'viewport' and therefore there
                     * isn't a chance it intersects with the ClipRectangle. But if the
                     * original cell location was (100, 100) and the scrolling offset 
                     * size was (-100,-100) that would draw the cell at (0,0) of the 
                     * control. So the rule is that if the final location of the cell
                     * to be drawn is not negative, then it is within the viewport and
                     * MAY be redrawn if it intersects with the ClipRectangle.
                     */
                    
                    Point cellLocation = new Point(col * cellWidth + scrollOffset.Width, 
                                                   row * cellHeight + scrollOffset.Height);
 
                    // Define cell parameter
                    Rectangle cellRect = new Rectangle(cellLocation.X,
                                                       cellLocation.Y,
                                                       cellWidth, cellHeight);
 
                    // Only draw cell if it needs to be painted
                    if (cellRect.IntersectsWith(e.ClipRectangle))
                    {
                        g.FillRectangle(new SolidBrush(_cellColor), cellRect);
                        g.DrawRectangle(new Pen(_borderColor, 1), cellRect);
                    }
 
                    
                }
            }
        }
 
        public void SetControlSize()
        {
            // 1 is added otherwize the most very most right line and
            // the very most bottom line will not show.
            // this.Size = new Size(_width * _cols + 1, _height * _rows + 1);
 
            this.AutoScrollMinSize = new Size(this.CellWidth * 
                                     this.Columns , this.CellHeight * this.Rows );
            this.Invalidate(); // Redraw the whole control
        }
 
        [Browsable(true), 
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Description("The number of columns in the grid.")]
 
        public int Columns
        {
            get { return _cols; }
            set
            {
                _cols = value;
                this.SetControlSize();
            }
        }
 
        [Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Description("The number of rows in the grid.")]
        public int Rows
        {
            get { return _rows; }
            set
            {
                _rows = value;
                this.SetControlSize();
            }
        }
 
        [Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Description("The width, in pixels, of one cell.")]
        public int CellWidth
        {
            get { return _width; }
            set
            {
                _width = value;
                this.SetControlSize();
            }
        }
 
        [Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), 
         Description("The height, in pixels, of one cell.")]
        public int CellHeight
        {
            get { return _height; }
            set
            {
                _height = value;
                this.SetControlSize();
            }
        }
 
        public float Zoom
        {
            get { return _zoom; }
            set
            {
                _zoom = value;
                this.SetControlSize();
            }
        }
 
        [Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Description("Height & width of this control")]
        public new Size Size
        {
            get { return base.Size; }
            set { base.Size = value; }
        }
 
        [Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Description("The color of the background inside of the cell.")]
        public Color CellColor
        {
            get { return _cellColor; }
            set
            {
                _cellColor = value;
                this.Invalidate();
            }
        }
 
        [Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Description("The color of the border around the cell.")]
        public Color BorderColor
        {
            get { return _borderColor; }
            set
            {
                _borderColor = value;
                this.Invalidate();
            }
        }
 
        private void GridCustomControl_Scroll(object sender, ScrollEventArgs e)
        {
            //if (e.Type.ToString() != "ThumbTrack")
            //{
            //    Console.WriteLine(e.Type.ToString());
            //    Console.WriteLine(e.ScrollOrientation.ToString());
            //    Console.WriteLine(e.OldValue.ToString());
            //    Console.WriteLine(e.NewValue.ToString());
            //    Console.WriteLine("END");
            //    //this.Invalidate();
            //}
            //else
            //{
            //    Console.WriteLine(e.Type.ToString());
            //    Console.WriteLine(e.ScrollOrientation.ToString());
            //    Console.WriteLine(e.OldValue.ToString());
            //    Console.WriteLine(e.NewValue.ToString());
            //    Console.WriteLine("END");
            //    this.Invalidate();
            //}
        }
 
        
 
        protected override void WndProc(ref Message m)
        {
            if ((m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL)
                && (((int)m.WParam & 0xFFFF) == 5))
            {
                // Change SB_THUMBTRACK to SB_THUMBPOSITION
                m.WParam = (IntPtr)(((int)m.WParam & ~0xFFFF)
                           | 4);
            }
            base.WndProc(ref m);
        }
    }
}
[+][-]06/27/08 12:02 PM, ID: 21886699Accepted Solution

View this solution now by starting your 30-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

Zones: C# Programming Language, Microsoft Visual C#.Net, .Net Editors & IDEs
Tags: c# csharp c sharp .net
Sign Up Now!
Solution Provided By: RishadanPort
Participating Experts: 1
Solution Grade: A
 
[+][-]06/27/08 10:26 AM, ID: 21885952Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 10:33 AM, ID: 21886013Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 10:56 AM, ID: 21886210Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:09 AM, ID: 21886314Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:12 AM, ID: 21886340Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 11:19 AM, ID: 21886400Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:21 AM, ID: 21886421Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 11:27 AM, ID: 21886465Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 11:29 AM, ID: 21886482Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:32 AM, ID: 21886499Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 11:35 AM, ID: 21886518Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:36 AM, ID: 21886520Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 11:38 AM, ID: 21886543Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:39 AM, ID: 21886548Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 11:39 AM, ID: 21886555Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 11:41 AM, ID: 21886559Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:42 AM, ID: 21886565Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:46 AM, ID: 21886591Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:47 AM, ID: 21886598Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 11:50 AM, ID: 21886624Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/27/08 12:00 PM, ID: 21886687Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]06/27/08 12:05 PM, ID: 21886720Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
 
Loading Advertisement...
20091111-EE-VQP-92 / EE_QW_2_20070628