c# : implement OnClick into Object

  • Thread starter Thread starter W. Wandrey
  • Start date Start date
W

W. Wandrey

Hallo,

I'm a newbie and try to learn C# while trying to progr. a
little game which in affect touches all aspects of the language:
syntax, arrays, painting, events .... so on events I be stuck:

is it possible (and how) to implement an OnClick-Event into a
class derived from :Object. i know it's easy to have it derived from
UserControls/Controls and higher Objects - but that seems for me
to have to much overhead for a simple Object.
Background:
Have a class that paints a rectangle on constructors call with some
other properties set. At least I need to have many of these rectangles.
Also any of them needs to have a OnClick to be identified an moved
at Click-event. How is this event to implement - anybody can give some
code-examples, please?

Regards
Willy
 
Hi,

You've go to add Click event:

public event EventHandler Click;

And you should implement very useful protected method,
that will raise that event:

protected virtual void OnClick(EventArgs e) {
if( this.Click!=null ) {
this.Click(e);
}
}

The problem is that you'll have to find a way to
call "OnClick" method. Buttons have a "PerformClick"
method to do this in standard.

Cheers!

Marcin
 
Short summery,
sorry I have no imagine to "Create Custom Event" which handles MouseClicks
on Objects that Paint a Rectangle???
 
Back
Top