--- title: "Overriding the OnPaint Method" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "Paint event [Windows Forms], handling in Windows Forms custom control" - "OnPaint method [Windows Forms], overriding in Windows Forms custom controls" ms.assetid: e9ca2723-0107-4540-bb21-4f5ffb4a9906 --- # Overriding the OnPaint Method The basic steps for overriding any event defined in the .NET Framework are identical and are summarized in the following list. #### To override an inherited event 1. Override the protected `On`*EventName* method. 2. Call the `On`*EventName* method of the base class from the overridden `On`*EventName* method, so that registered delegates receive the event. The event is discussed in detail here because every Windows Forms control must override the event that it inherits from . The base class does not know how a derived control needs to be drawn and does not provide any painting logic in the method. The method of simply dispatches the event to registered event receivers. If you worked through the sample in [How to: Develop a Simple Windows Forms Control](how-to-develop-a-simple-windows-forms-control.md), you have seen an example of overriding the method. The following code fragment is taken from that sample. ```vb Public Class FirstControl Inherits Control Public Sub New() End Sub Protected Overrides Sub OnPaint(e As PaintEventArgs) ' Call the OnPaint method of the base class. MyBase.OnPaint(e) ' Call methods of the System.Drawing.Graphics object. e.Graphics.DrawString(Text, Font, New SolidBrush(ForeColor), RectangleF.op_Implicit(ClientRectangle)) End Sub End Class ``` ```csharp public class FirstControl : Control { public FirstControl() {} protected override void OnPaint(PaintEventArgs e) { // Call the OnPaint method of the base class. base.OnPaint(e); // Call methods of the System.Drawing.Graphics object. e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle); } } ``` The class contains data for the event. It has two properties, as shown in the following code. ```vb Public Class PaintEventArgs Inherits EventArgs ... Public ReadOnly Property ClipRectangle() As System.Drawing.Rectangle ... End Property Public ReadOnly Property Graphics() As System.Drawing.Graphics ... End Property ... End Class ``` ```csharp public class PaintEventArgs : EventArgs { ... public System.Drawing.Rectangle ClipRectangle {} public System.Drawing.Graphics Graphics {} ... } ``` is the rectangle to be painted, and the property refers to a object. The classes in the namespace are managed classes that provide access to the functionality of GDI+, the new Windows graphics library. The object has methods to draw points, strings, lines, arcs, ellipses, and many other shapes. A control invokes its method whenever it needs to change its visual display. This method in turn raises the event. ## See also - [Events](/dotnet/standard/events/index) - [Rendering a Windows Forms Control](rendering-a-windows-forms-control.md) - [Defining an Event](defining-an-event-in-windows-forms-controls.md)