Quick and to the issue -- THanks.
Main Topics
Browse All TopicsSetup for Chessboard:
I have a new Windows Applicaiton in VS 2005 using C#. Then I added a user control to the project and placed it on the form. On this control, I want to draw a grid that resizes. What triggers the paint event for the control? There is presently no code in the form.cs file. The following code is in the user control: (some lines are commented out while I am working on parts but I can see in debug that this event is not being fired. I welcome any help on this code and the trigger for it)
using System;
using System.Collections.Generic
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace Chess
{
public partial class GamePanel : UserControl
{
public GamePanel()
{
InitializeComponent();
// bit flag enum
this.SetStyle(ControlStyle
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer
ControlStyles.ResizeRedraw
}
private void GamePanel_Load(object sender, EventArgs e)
{
}
private void GamePanel_Paint(object sender, PaintEventArgs e)
{
// string alignment -- center.
//StringFormat sf = new StringFormat();
//sf.Alignment = StringAlignment.Center;
//sf.LineAlignment = StringAlignment.Center;
//Pen black = new Pen(Color.Black);
// this.ClientRectangle;
e.Graphics.DrawLine(Pens.B
// anchor control
// this.ClientRectangle.Right
// this.ClientRectangle.Botto
// draw grid
float rowHt = this.ClientRectangle.Heigh
for (int row = 1; row < 9; row ++){
Rectangle r = new Rectangle(0, (int)(row * rowHt),
this.ClientRectangle.Width
e.Graphics.DrawLine(Pens.B
}
// Brush base = new SolidBrush Brush(Color, Fromargb(12, 240, 210, 0);
// e.Graphics.DrawString("TES
}
private void GamePanel_MouseDown(object
{
}
private void GamePanel_MouseUp(object sender, MouseEventArgs e)
{
}
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
how about some tutoring?
The syntax is unfamiliar to me. I thought there was a typo in the +=
I tried just =
error.
I don't understand what this line says. Isn't it the same as
this.Paint = this.Paint + GamePanel_Paint;
'this' being a pointer/reference to the current object/control Paint method. So how are these methods combined? What does that mean? and why is the paint event for a control described as in the erro msg below? Would you expand on this further?
The event 'System.Windows.Forms.Cont
Does this create a chain of delegates? If so, is the purpose then to establish a sequence of execution? Perhaps this += is an operator overload that permits the delegate classes to be combined into a chain. So, any invocation of a method implicity calls the next delegate in the chain to be called in sequence.
any comments welcome
'Paint' isn't a method, it's an event.
From 'outside' the class, you add handlers to the event by using +=, and remove them by using -=.
Although "x += y" is usually the same as doing "x = x + y", that's not the case for events - the += and -= are special - they add/remove 'y' to the existing chain of handlers (delegates) that are attached to the evnet variable.
From 'inside' the class, you trigger the event when appropriate, using something like
if ( Paint != null )
{
Paint( <arguments go here> )
}
this calls any handler(s) that have been attached to that event.
The UserControl base class is already doing this for you at the appropraite points internally, hence anything attahced to the 'Paint' event is called when the control needs repainting.
Events are special. The ONLY operations you can do to an event outside of the class that's its defined in are the special += and -=.
The alteranative method is to override the OnPaint method, since you're deriving from UserControl. Then you don't need to attach a handler - just implement the paint functionality you want. Note - I got the method name wrong in my original post. The method to override is called 'OnPaint', not just 'Paint'.
Business Accounts
Answer for Membership
by: andrewjbPosted on 2007-04-13 at 01:20:48ID: 18904022
Either attach the event handler to yourself in the constructor
this.Paint += GamePanel_Paint;
or, rather than an event handler, override the paint method
public override Paint(...)
{...}