views:

31

answers:

1

Hello,

I'm new to C# but not to OOP.

I'd like to make a "canvas" panel on which a user can draw shapes by mouseClick-ing but also delete them (nothing fancy, fixed sizes and whatnot, plain old pen objects). Like I said, I want the user to be able to delete whatever objects he alt-clicks on.

I'm not sure how exactly could I go about doing this. If I were using Flash, I'd probably do something like:

my_circle_object = new disc-or-whatever-etc;
canvas.addChild(my_circle_object);
my_circle_object.AddEventListener(MouseClickEvent, function_to_remove_child);

Now, since compiled languages are the devil when it comes to simple front-end UI related stuff, I'm sure It'll take 20 times more code to write this in C#. But, is there anything similar to my example?

I've spent all afternoon reading on things like GraphicsContainers, SmoothingPaint, Graphics Persistence using bitmaps etc. but I never found a simple add event method..

Thank you

A: 

The objects that you draw using the shape methods on a Graphics object (e.g. DrawLine, DrawEllipse, DrawRect, etc.) do not represent conceptual objects as far as the graphics API is concerned. Calling those functions simply draws the item to the graphics surface as a bitmap. Once that's done, there's nothing there to attach an event to.

You'll need to create your own shape types and have them draw themselves to the graphics object. You'll have to attach to the appropriate mouse events on whatever control you're using (I'm assuming a Panel) and do your own collision detection.

Adam Robinson
Thanks for your answer. So should I make a, say, Circle class that creates a new graphics object with an ellipse in it and then add circle_instance to the Panel? (if so, how do I add an object to a Panel through code in C#?)
Twodordan
@Twodordan: No, you'd create a `Circle` object that contains the information *about* the circle (location, diameter, color, etc.), then when the `Panel`'s `Paint` event fires, you'd pass the `Graphics` object supplied in that event to all of the various shapes in your `Panel` (you'd need to maintain your own list somewhere) and allow them to draw themselves on the supplied `Graphics` object.
Adam Robinson
Thank you. So that's how it works.
Twodordan