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
 
 
 
 
 
TitleDraw a centered circle whenever the user resizes a form in VB .NET
DescriptionThis example shows how 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 uses the form's CreateGraphics method to make a Graphics object for the form and passes it to the DrawCircle method.

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
    DrawCircle(Me.CreateGraphics)
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