?
Solved

problem abstracting border drawing

Posted on 2003-10-29
12
Medium Priority
?
334 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 1000 total points
ID: 9650742
then in the derived class call 'base.OnPaint' before doing any other drawing
0
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

762 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