I'm trying to draw a simple ellipse on the form after pressing a button. It works when I have the ellipse in the upper left hand corner of the screen, but doesn't show up anywhere else. I've noticed that the location it does show up in was the original size of the form before I expanded it. Does anyone know how to fix this?
Oh, thanks a bunch. Is there a way to call that from a button? It starts animating immediately after the program starts, can I make it start after pressing a button? Also, it seems to loop infinitely
Almost all the drawings should be done in the controls’ Paint event, because when you minimize of maximize the form it is being redrawn so the drawing you have is being erased. But if you draw it in the form’s Paint event it is being erased and redrawn.
Yes you can do it from the button click. You just need to have a Boolean flag to prevent or allow the drawing. Here is an example:
vb Code:
Public Class Form1
Dim draw As Boolean
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If Me.draw Then
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(5, 5, 40, 40))
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.draw = True
Me.Refresh()
End Sub
End Class
Rating is a way of saying thank you. Don't forget to rate always!
Ok, but the circles still get cut off. Let me start over. This is all I'm trying to do:
Code:
Public Class Form1
Dim gr As Graphics = CreateGraphics()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Mult()
End Sub
Private Sub Mult()
gr.DrawEllipse(Pens.Red, New Rectangle(300, 400, 40, 40))
gr.DrawEllipse(Pens.Red, New Rectangle(400, 400, 40, 40))
End Sub
End Class
Click a button, it calls a function, which draws a couple circles on the form. When I do this, the circles don't show up on the entire screen - they only show in the upper left
I don't know what you mean by cuts off. The circles draw where you specify. Here is the code where i draw multiple circles as you can see them in the image:
vb Code:
Public Class Form1
Dim draw As Boolean
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If Me.draw Then
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(5, 5, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(10, 10, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(15, 15, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(20, 20, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(25, 25, 40, 40))
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.draw = True
Me.Refresh()
End Sub
End Class
Rating is a way of saying thank you. Don't forget to rate always!
Public Class Form1
Dim gr As Graphics = CreateGraphics()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Mult()
End Sub
Private Sub Mult()
gr.DrawEllipse(Pens.Red, New Rectangle(200, 250, 40, 40))
gr.DrawEllipse(Pens.Red, New Rectangle(275, 200, 40, 40))
End Sub
End Class
Let me guess what is hapening here. You use that code and draw the circles and then you maximize the form with the mouse so that you see the half circles! That is why i sayd that you need to draw the circles in the paint event and use the paint event's e.Graphics object to draw. It simms you didn't pay attention to that. Try this:
vb Code:
Public Class Form1
Dim draw As Boolean
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If Me.draw Then
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(200, 250, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(275, 200, 40, 40))
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.draw = True
Me.Refresh()
End Sub
End Class
Rating is a way of saying thank you. Don't forget to rate always!
Thats exactly what I was looking for, thanks for your time. If you're wondering, the program is supposed to mimic planetary motion - orbits, gravitational catapults, all that good stuff. Thanks again