
306
LESSON 25 Fine-Tuning Classes
In the form’s
Load event handler, create some ShapeRectangles and ShapeEllipses and
add them to the list.
Give the form a
CheckBox labeled Colored. In its CheckedChanged event handler, invalidate
the form to force a redraw.
In the form’s
Paint event handler, redraw the form by looping through the Shapes in the
list. If the
CheckBox is checked, invoke the Shape objects’ Draw method, passing it the pen
and brush
Pens.Blue and Brushes.LightBlue. If the CheckBox is not checked, invoke the
Draw method with no parameters.
Hints
If gr is the Graphics object, then you can use these methods to draw:
gr.Clear(this.BackColor)
— Clears the object with the form’s background color.
gr.FillRectangle(
brush, rect) — Fills a rectangle defined by the Rectangle rect with
brush.
gr.DrawRectangle(
pen, rect) — Outlines a rectangle defined by the Rectangle rect
with
pen.
gr.FillEllipse(
brush, rect) — Fills an ellipse defined by the Rectangle rect with brush.
gr.DrawEllipse(
pen, rect) — Outlines an ellipse defined by the Rectangle rect with pen.
In the
Paint event handler, draw on the Graphics object provided by the event handler’s
e.Graphics parameter.
Step-by-Step
Start a new program and create the
Shape class.
Give the class a
using System.Drawing directive.
1. Simply place the using directive at the top of the file.
Give the class a property named
Bounds