Solved

problem abstracting border drawing

Posted on 2003-10-29
12
330 Views
Last Modified: 2010-04-16
ref. the code below
have a class that draws a border around itself
want a base class that handles the border drawing

:: here the border gets drawn ::

public MyClass
{
     public MyClass()
     {
     }

     protected override void OnResize(EventArgs e)
     {
          this.Invalidate();
     }

     protected override void OnPaint(PaintEventArgs e)
     {
          Graphics g = e.Graphics;

          /* draw shape */

          /* draw border */
     }
}

:: here it never gets drawn ::

public BaseClass
{
     public BaseClass()
     {
          this.Invalidated += new InvalidateEventHandler(this.DrawBorder);
     }

     protected void DrawBorder(object sender, InvalidateEventArgs e)
     {
          Graphics g = Graphics.FromWhnd(this.Handle);

          /* draw border */
     }
}

:: solution ::

that's why I'm here :D
0
Comment
Question by:cuynen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 11

Expert Comment

by:Agarici
ID: 9650310
try adding your border drawing  to the paint event
0
 
LVL 2

Author Comment

by:cuynen
ID: 9650641
:: subscribing to the Paint event ::

public BaseClass
{
     public BaseClass()
     {
          this.Paint += new PaintEventHandler(this.DrawBorder);
     }

     protected void DrawBorder(object sender, PaintEventArgs e)
     {
          Graphics g = e.Graphics;

          /* draw border */
     }
}

:: thoughts ::

it doesn't work because I've overridden the protected OnPaint method in the derived class
if I comment out the overridden method, the border is drawn
but of course then my derived class serves no purpose anymore :)
0
 
LVL 11

Accepted Solution

by:
Agarici earned 250 total points
ID: 9650742
then in the derived class call 'base.OnPaint' before doing any other drawing
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:cuynen
ID: 9650926
so there is no way the base class can auto-draw the border after the derived is finished painting?
0
 
LVL 11

Expert Comment

by:Agarici
ID: 9650954
the 'OnPaint' method in the base class wont get called unless you call 'base.OnPaint'
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9656224
How about this:

public MyClass
{
    public MyClass()
    {
    }

    protected override void OnResize(EventArgs e)
    {
         this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
         Graphics g = e.Graphics;

         /* draw shape */

        base.OnPaint(e);
    }
}


public BaseClass
{
    public BaseClass()
    {
    }

    protected override void OnPaint(PaintEventArgs e)
    {
         Graphics g = e.Graphics;

         /* draw border */
    }
}
0
 
LVL 2

Author Comment

by:cuynen
ID: 9656268
yes, that's what I had after Agarici's second comment
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9656326
You could use cuynen's variant.
0
 
LVL 3

Expert Comment

by:WiB
ID: 9659071
Any special reason to draw the border in base class?
0
 
LVL 2

Author Comment

by:cuynen
ID: 9659335
so all subclasses will have a border drawn around them
0
 
LVL 2

Author Comment

by:cuynen
ID: 9659336
*** around their visual representation, that is
0
 
LVL 3

Expert Comment

by:WiB
ID: 9660538
The code in comment from  ptmcomp
Date: 10/31/2003 01:03AM PST  
should work perfectly.

 
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# DateTime Format validation 4 88
Stop Git from being my repository 1 45
Building an object from several other objects 4 36
async questions 5 46
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question