Private rect As New Rectangle(0, 0, 25, 50)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Get the area that was previously drawn in.
Dim oldRect As Rectangle = Me.rect
'Move the drawing area.
Me.rect.Offset(5, 10)
'Get the area that is now going to be drawin in.
Dim newRect As Rectangle = Me.rect
'Make each area for invalidation 1 pixel larger than the actual drawing area in every direction
oldRect.Inflate(1, 1)
newRect.Inflate(1, 1)
'Notify the form that the old and new areas require repainting.
Me.Invalidate(oldRect)
Me.Invalidate(newRect)
'Tell the form to repaint invalidated areas.
Me.Update()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
With e.Graphics
.FillRectangle(Brushes.Red, Me.rect)
.DrawRectangle(Pens.Black, Me.rect)
End With
End Sub