Results 1 to 3 of 3

Thread: Question about overriding

  1. #1

    Thread Starter
    Addicted Member A.P.Gumby's Avatar
    Join Date
    Oct 2005
    Location
    Outside the assylum
    Posts
    147

    Question about overriding

    I can find examples on how to override the form paint event but how would I go about overriding a panel paint event?

    Any help appreciated.
    Using Visual Studio 2008.
    I am not young enough to know everything - Oscar Wilde

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Question about overriding

    You don't override events. You handle events and you override methods. If you want to provide custom painting functionality for a particular Panel on a particular form then you can handle its Paint event, just like you would for the form itself:
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
    4.         'Provide your own custom logic here, e.g. draw a diagonal line.
    5.         e.Graphics.DrawLine(Pens.Black, 0, 0, Me.Width, Me.Height)
    6.     End Sub
    7.  
    8. End Class
    Alternatively, and especially if you want to provide this same functionality for more than one Panel on more than one form, then you should declare your own class, inherit the Panel class and override its OnPaint method. It is the OnPaint method that raises the Paint event, so you invoke the standard functionality first and then you provide your own custom logic, e.g.
    VB Code:
    1. Public Class PanelEx
    2.     Inherits Panel
    3.  
    4.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    5.         'Invoke the standard functionality of the Panel class, including raising the Paint event.
    6.         MyBase.OnPaint(e)
    7.  
    8.         'Provide your own custom logic here, e.g. draw a diagonal line.
    9.         e.Graphics.DrawLine(Pens.Black, 0, 0, Me.Width, Me.Height)
    10.     End Sub
    11. End Class
    You then add instances of your own PanelEx class to your forms instead of vanilla Panel objects.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member A.P.Gumby's Avatar
    Join Date
    Oct 2005
    Location
    Outside the assylum
    Posts
    147

    Re: Question about overriding

    Thanks for the reply and my apologies for the nomenclature error.
    I am plotting a scrolling graph on a panel and I needed to override the Paint method in order to eliminate flicker.
    Using Visual Studio 2008.
    I am not young enough to know everything - Oscar Wilde

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width