Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.Net - how to draw a circle on form and keep it centered 3

Status
Not open for further replies.

rgreiser

Programmer
Mar 12, 2003
6
US
How would I draw a circle on a form in VB.Net and keep it centered if the form is resized?

Thanks,
Rhonda
 
Code:
    Dim mPreviousCircle As Rectangle
    Dim mNewCircle As Rectangle

        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = Me.CreateGraphics
        Dim p As New Pen(Me.BackColor)
        g.DrawEllipse(p, mPreviousCircle)

        Dim w As Single = 50
        Dim h As Single = 50
        Dim x As Single = (Me.ClientSize.Width / 2) - (w / 2)
        Dim y As Single = (Me.ClientSize.Height / 2) - (h / 2)

        mNewCircle = New Rectangle(x, y, w, h)
        mPreviousCircle = New Rectangle(x, y, w, h)

        p = New Pen(Color.OrangeRed)
        g.DrawEllipse(p, mNewCircle)
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Form1_Paint(Nothing, Nothing)
    End Sub
 
From a 'good coding practice' OOP point-of-view I suppose it's better to have the math outside the event as you well know but I just knocked it together quickly just for the chance to do some graphics instead of financial number crunching. (...it's getting pretty bad when you jump at the chance to draw circles)

Quick question Rick if you're still there...

in the call to the Form1_Paint event (from the resize event) I set the sender and e params to Nothing because I don't know a way to convert System.EventArgs to System.Windows.Forms.PaintEventArgs.
Can this be done?
 
of course you could also do this.

Code:
Dim mPreviousCircle As Rectangle
    Dim mNewCircle As Rectangle

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

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

    Private sub CreateGraphics()
        Dim g As Graphics = Me.CreateGraphics
        Dim p As New Pen(Me.BackColor)
        g.DrawEllipse(p, mPreviousCircle)

        Dim w As Single = 50
        Dim h As Single = 50
        Dim x As Single = (Me.ClientSize.Width / 2) - (w / 2)
        Dim y As Single = (Me.ClientSize.Height / 2) - (h / 2)

        mNewCircle = New Rectangle(x, y, w, h)
        mPreviousCircle = New Rectangle(x, y, w, h)

        p = New Pen(Color.OrangeRed)
        g.DrawEllipse(p, mNewCircle)
    End Sub

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Thank you all so much. It has been very helpful. I got it working yesterday. That formula really helped because I kept getting close to center, but forgot to add in the size of the circle. I also couldn't figure out how to ignore the title bar because without the clientsize the circle stayed higher than I wanted. And that was a good idea about the sub procedure to keep from retyping code.

Rhonda
 
One thing that is worth mentioning is that instead of using Me.CreateGraphics, I would suggest passing in the graphics object that is passed into the Paint event.
 
Yep sorry I didn't realise I did use the same name just change it to sub_creategraphics.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top