Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have make an ellipse
i try to call the event .click
but it fail
cause i am try to make a round circle.
can anyone teach me how to addhandle to a shape?

What I have tried:

myPen = New Pen(Drawing.Color.Red, 5)

Dim myGraphics As Graphics = Me.CreateGraphics

Dim myRectangle As New Rectangle

myRectangle.X = 10

myRectangle.Y = 10

myRectangle.Width = 200

myRectangle.Height = 200

myGraphics.DrawEllipse(myPen, myRectangle)
Posted
Updated 2-Jun-16 8:24am
Comments
Sergey Alexandrovich Kryukov 2-Jun-16 13:38pm    
You need to tag the application type and the UI framework/library you are using.
It looks like you are using System.Windows forms with System.Drawing. If so, you do it all wrong and formulate the question in a wrong way: you don't have an Ellipse object. You have to handle events at the level of the control. And your rendering is totally wrong. You don't handle any events. You don't dispose IDisposable objects. But you did not provide a representative code sample.

I can help you if you explain properly what you are trying to achieve.

—SA

1 solution

There are a couple of problems here - the first is that you are creating your own Graphics object, but you aren't releasing it. That's bad: Graphics handles are a scarce resource, and if you do that too often, your app will crash. I'd recommend a Using block:
VB
Using myGraphics As Graphics = Me.CreateGraphics
   Dim myRectangle As New Rectangle
   myRectangle.X = 10
   myRectangle.Y = 10
   myRectangle.Width = 200
   myRectangle.Height = 200
   myGraphics.DrawEllipse(myPen, myRectangle)
End Using
As it will handle it all for you.
Secondly, that will draw an ellipse - once. But it won't be "persistent" - if you move another app over the top, the ellipse will disappear when you app is displayed again. To prevent that, move the drawing code into the Paint event handler, and use the Graphics context provided by the event argument. That means the ellipse is persisted, and you don't need to create (or destroy) your own graphics context.

Clicking the ellipse is another problem: only controls can respond to mouse events - so if you want to detect when the ellipse was clicked, you need to handle the Click event for the control you drew the ellipse onto, and check if the click is inside or outside the actual ellipse.
That's not as simple as it might seem, because although you know the dimensions of the bounding rectangle that defines the ellipse, not all of the area inside that rectangle is inside the ellipse, and you don't know which parts are!
To do that, you need to create an elliptical path and test that:
VB.NET
Private Sub myDrawing_MouseClick(sender As Object, e As MouseEventArgs)
	Dim path As New GraphicsPath()
	path.AddEllipse(0, 0, 200, 100)
	If path.IsVisible(e.Location) Then
		Console.WriteLine("YES")
	Else
		Console.WriteLine("NO")
	End If
End Sub
 
Share this answer
 
Comments
rolandoelninoz 3-Jun-16 7:50am    
thank you bro!
bro what is GraphicsPath
OriginalGriff 3-Jun-16 8:22am    
It's a .NET class:
https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath(v=vs.110).aspx
It's to do with shapes that aren't simple rectangles or squares.

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