Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the Invalidate method to draw a centered circle whenever the user resizes a form in VB .NET
DescriptionThis example shows how to use the Invalidate method to draw a centered circle whenever the user resizes a form in VB .NET.
KeywordsVB.NET, circle, center, resize, Paint, GDI+
CategoriesVB.NET, Graphics
 
The Form's Paint event handler calls subroutine DrawCircle, passing it the Graphics object with which the form should draw itself.

The Resize event handler calls the form's Invalidate method. That generates a Paint event for the form's client area so the Paint event handler calls DrawCircle.

Subroutine DrawCircle clears the form (see what happens if you comment out that line) and then uses the Graphics object it was passed to draw a circle. It also draws an X on the form so it's easy to see that the circle is centered.

 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    DrawCircle(e.Graphics)
End Sub

Private Sub Form1_Resize(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles MyBase.Resize
    Me.Invalidate()
End Sub

Private Sub DrawCircle(ByVal gr As Graphics)
    Const RADIUS As Integer = 20
    Dim x As Integer = Me.ClientSize.Width \ 2 - RADIUS
    Dim y As Integer = Me.ClientSize.Height \ 2 - RADIUS
    gr.Clear(Me.BackColor)
    gr.DrawArc(Pens.Blue, x, y, 2 * RADIUS, 2 * RADIUS, 0, _
        360)
    gr.DrawLine(Pens.Red, 0, 0, Me.ClientSize.Width, _
        Me.ClientSize.Height)
    gr.DrawLine(Pens.Red, 0, Me.ClientSize.Height, _
        Me.ClientSize.Width, 0)
End Sub
 
For more information about graphics programming in Visual Basic 6, see my book Ready-to-Run Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated