Click here to Skip to main content
15,886,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add different circles on the form but user will tell the size of and color of the circle.
How can i do this??

Here what i try!

VB
Private Sub Form3_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    'Create(Pens)
    Dim g As Graphics
    g = Panel1.CreateGraphics
    Dim yellowPen As New Pen(Color.Yellow, 20)
    Dim bluePen As New Pen(Color.Blue, 30)
    Dim greenPen As New Pen(Color.Green, 20)
    Dim skybluePen As New Pen(Color.AliceBlue, 20)
    Dim voiletPen As New Pen(Color.Violet, 15)
    Dim blackPen As New Pen(Color.Black, 2)
    ' Create a rectangle
    Dim rect As New Rectangle(80, 80, 50, 50)
    ' Draw ellipses
    g.DrawEllipse(yellowPen, 260, 180, 10, 10)

    g.DrawEllipse(greenPen, 240, 160, 50, 50)

    g.DrawEllipse(bluePen, 220, 140, 90, 90)

    g.DrawEllipse(greenPen, 200, 120, 130, 130)

    g.DrawEllipse(skybluePen, 180, 100, 170, 170)

    g.DrawEllipse(blackPen, 180, 100, 170, 170)

    g.DrawEllipse(voiletPen, 170, 90, 190, 190)

  End Sub
Posted
Updated 8-Jul-14 23:41pm
v2

1 solution

First off, If you create a Graphics object, you are responsible for Disposing of it - and if you don't there will be problems. They are scarce resources and will run out long before your get low on memory and the Garbage Collector gets called in to sort it out.

Why are you drawing in the Panel via the Form Paint?
Either draw on the Form in the Form Paint event handler, or in the Panel in the Panel Paint event handler - and in both cases you are then passed the appropriate Graphics context read to use in teh PaintEventArgs parameter.
So to paint on the Form:
VB
Private Sub Form3_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    'Create(Pens)
    Dim g As Graphics = e.Graphics
...
    g.DrawEllipse(yellowPen, 260, 180, 10, 10)
And the same code will draw on the panel in it's Paint handler without modification.
 
Share this answer
 
Comments
Yellow_Flash 10-Jul-14 0:37am    
thanks for your the answer
It really help me out! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900