--- title: "User-Drawn Controls" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "custom controls [Windows Forms], user-drawn" - "OnPaint method [Windows Forms]" - "user-drawn controls [Windows Forms]" ms.assetid: 034af4b5-457f-4160-a937-22891817faa8 --- # User-Drawn Controls The .NET Framework provides you with the ability to easily develop your own controls. You can create a user control, which is a set of standard controls bound together by code, or you can design your own control from the ground up. You can even use inheritance to create a control that inherits from an existing control and add to its inherent functionality. Whatever approach you use, the .NET Framework provides the functionality to draw a custom graphical interface for any control you create. Painting of a control is accomplished by the execution of code in the control's method. The single argument of the method is a object that provides all of the information and functionality required to render your control. The provides as properties two principal objects that will be used in the rendering of your control: - object - the rectangle that represents the part of the control that will be drawn. This can be the entire control, or part of the control depending on how the control is drawn. - object - encapsulates several graphics-oriented objects and methods that provide the functionality necessary to draw your control. For more information on the object and how to use it, see [How to: Create Graphics Objects for Drawing](../advanced/how-to-create-graphics-objects-for-drawing.md). The event is fired whenever the control is drawn or refreshed on the screen, and the object represents the rectangle in which painting will take place. If the entire control needs to be refreshed, the will represent the size of the entire control. If only part of the control needs to be refreshed, however, the object will represent only the region that needs to be redrawn. An example of such a case would be when a control was partially obscured by another control or form in the user interface. When inheriting from the class, you must override the method and provide graphics-rendering code within. If you want to provide a custom graphical interface to a user control or an inherited control, you can also do so by overriding the method. An example is shown below: ```vb Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) ' Call the OnPaint method of the base class. MyBase.OnPaint(e) ' Declare and instantiate a drawing pen. Using myPen As System.Drawing.Pen = New System.Drawing.Pen(Color.Aqua) ' Draw an aqua rectangle in the rectangle represented by the control. e.Graphics.DrawRectangle(myPen, New Rectangle(Me.Location, Me.Size)) End Using End Sub ``` ```csharp protected override void OnPaint(PaintEventArgs e) { // Call the OnPaint method of the base class. base.OnPaint(e); // Declare and instantiate a new pen. using (System.Drawing.Pen myPen = new System.Drawing.Pen(Color.Aqua)) { // Draw an aqua rectangle in the rectangle represented by the control. e.Graphics.DrawRectangle(myPen, new Rectangle(this.Location, this.Size)); } } ``` The preceding example demonstrates how to render a control with a very simple graphical representation. It calls the method of the base class, it creates a object with which to draw, and finally draws an ellipse in the rectangle determined by the and of the control. Although most rendering code will be significantly more complicated than this, this example demonstrates the use of the object contained within the object. Note that if you are inheriting from a class that already has a graphical representation, such as or , and you do not wish to incorporate that representation into your rendering, you should not call your base class's method. The code in the method of your control will execute when the control is first drawn, and whenever it is refreshed. To ensure that your control is redrawn every time it is resized, add the following line to the constructor of your control: ```vb SetStyle(ControlStyles.ResizeRedraw, True) ``` ```csharp SetStyle(ControlStyles.ResizeRedraw, true); ``` > [!NOTE] > Use the property to implement a non-rectangular control. ## See also - - - - - - [How to: Create Graphics Objects for Drawing](../advanced/how-to-create-graphics-objects-for-drawing.md) - [Constituent Controls](constituent-controls.md) - [Varieties of Custom Controls](varieties-of-custom-controls.md)