Solved

problem abstracting border drawing

Posted on 2003-10-29
12
331 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

719 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