Multiple Objects Event Handlers?????

  • Thread starter Thread starter Gianmaria
  • Start date Start date
G

Gianmaria

hi,
that's my problem. I have an object and this have a onConnectionChanged
event. and when i use it on ny form i have also my event handler.

Now, my user can add many of this objects to a list ( that is rappresented
graphically by a listview), and each ListItem have in the tag property the
corresponding object. If i add manually a single object on my form.. i write
down manually the eventhandler for that object.. but.. how can i do.. if
objects are addd by a user.. and every object need his event handler???

p1.OnConnectionStatusChanged +=new
ConnectionStatusChangedEventHandler(p1_OnConnectionStatusChanged);

I need his for many objects.

Gianmaria
 
Hi Gianmara,

It's not entirely clear to me what you are trying to do, but if you automatically need to assign an event handler to run time objects:

1. Use the same eventhandler for all the objects.
2. Determine which object is used from the sender object.

for(int i = 0; i < 20; i++)
{
MyObject o = new MyObject();
o.SomeEvent += new SomeEventHandler(MyEventHandler);
// store o in some collection
}

private void MyEventHandler(object sender, SomeEventArgs e)
{
MyObject p = (MyObject)sender;
// p's status is changed
}

You can use the same event handler for as many object as you need.
 
Back
Top