Export (0) Print
Expand All
Expand Minimize

Control.OnPaint Method

Raises the Paint event.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Protected Overridable Sub OnPaint ( _
	e As PaintEventArgs _


Type: System.Windows.Forms.PaintEventArgs

A PaintEventArgs that contains the event data.

Raising an event invokes the event handler through a delegate. For more information, see Handling and Raising Events.

The OnPaint method also enables derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors

When overriding OnPaint in a derived class, be sure to call the base class's OnPaint method so that registered delegates receive the event.

The following code example enables the user to drag an image or image file onto the form, and have it be displayed at the point on which it is dropped. The OnPaint method is overridden to repaint the image each time the form is painted; otherwise the image would only persist until the next repainting. The DragEnter event-handling method determines the type of data being dragged into the form and provides the appropriate feedback. The DragDrop event-handling method displays the image on the form, if an Image can be created from the data. Because the DragEventArgs.X and DragEventArgs.Y values are screen coordinates, the example uses the PointToClient method to convert them to client coordinates.

Private picture As Image
Private pictureLocation As Point

Public Sub New()
   ' Enable drag-and-drop operations. 
   Me.AllowDrop = True 
End Sub 

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

   ' If there is an image and it has a location,  
   ' paint it when the Form is repainted. 
   If (Me.picture IsNot Nothing) And _
     Not (Me.pictureLocation.Equals(Point.Empty)) Then
      e.Graphics.DrawImage(Me.picture, Me.pictureLocation)
   End If 
End Sub 

Private Sub Form1_DragDrop(ByVal sender As Object, _
  ByVal e As DragEventArgs) Handles MyBase.DragDrop
   ' Handle FileDrop data. 
   If e.Data.GetDataPresent(DataFormats.FileDrop) Then 
      ' Assign the file names to a string array, in  
      ' case the user has selected multiple files. 
      Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
         ' Assign the first image to the 'picture' variable. 
         Me.picture = Image.FromFile(files(0))
         ' Set the picture location equal to the drop point. 
         Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
      Catch ex As Exception
      End Try 
   End If 

   ' Handle Bitmap data. 
   If e.Data.GetDataPresent(DataFormats.Bitmap) Then 
         ' Create an Image and assign it to the picture variable. 
         Me.picture = CType(e.Data.GetData(DataFormats.Bitmap), Image)
         ' Set the picture location equal to the drop point. 
         Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
      Catch ex As Exception
      End Try 
   End If 

   ' Force the form to be redrawn with the image. 
End Sub 

Private Sub Form1_DragEnter(ByVal sender As Object, _
  ByVal e As DragEventArgs) Handles MyBase.DragEnter
   ' If the data is a file or a bitmap, display the copy cursor. 
   If e.Data.GetDataPresent(DataFormats.Bitmap) _
      Or e.Data.GetDataPresent(DataFormats.FileDrop) Then
      e.Effect = DragDropEffects.Copy
      e.Effect = DragDropEffects.None
   End If 
End Sub
' This example creates a PictureBox control on the form and draws to it.  
' This example assumes that the Form_Load event handler method is connected  
' to the Load event of the form. 
Private pictureBox1 As New PictureBox()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' Dock the PictureBox to the form and set its background to white.
    pictureBox1.Dock = DockStyle.Fill
    pictureBox1.BackColor = Color.White
    ' Connect the Paint event of the PictureBox to the event handler method. 
    AddHandler pictureBox1.Paint, AddressOf Me.pictureBox1_Paint

    ' Add the PictureBox control to the Form. 
End Sub 'Form1_Load

Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    ' Create a local version of the graphics object for the PictureBox. 
    Dim g As Graphics = e.Graphics

    ' Draw a string on the PictureBox.
    g.DrawString("This is a diagonal line drawn on the control", _
        New Font("Arial", 10), Brushes.Red, New PointF(30.0F, 30.0F))
    ' Draw a line in the PictureBox.
    g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, _ 
        pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom)
End Sub 'pictureBox1_Paint

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft