Mark
Mark

Reputation: 5038

C#: Retrieve the source of a fired event without the sender object

I'm using the GMaps.NET Controls and I catch the event "TileLoadComplete":

http://www.nudoq.org/#!/Packages/GMap.NET.WindowsForms/GMap.NET.WindowsForms/GMapControl/E/OnTileLoadComplete

I create several controls at run-time and all of them share the same function for that event:

for (int i = 0; i < 5; i++)
{
    GMap.NET.WindowsForms.GMapControl control = new GMap.NET.WindowsForms.GMapControl();
    control.Manager.Mode = AccessMode.ServerOnly;
    control.MapProvider = GMap.NET.MapProviders.GoogleSatelliteMapProvider.Instance;
    control.OnTileLoadComplete += Control_OnTileLoadComplete;
    // set other map properties
}

private void Control_OnTileLoadComplete(long ElapsedMilliseconds)
{
    // who has completed the loading?
}

Because there is no sender object in the event signature, I wonder if there is another way to know which control has completed the loading of the map.

Upvotes: 0

Views: 157

Answers (1)

user47589
user47589

Reputation:

Would this work? Use a lambda to capture the sender:

for (int i = 0; i < 5; i++)
{
    GMap.NET.WindowsForms.GMapControl control = new GMap.NET.WindowsForms.GMapControl();
    //...snip...
    control.OnTileLoadComplete += x => Control_OnTileLoadComplete(control, x);
}

private void Control_OnTileLoadComplete(object sender, long ElapsedMilliseconds)
{
    // who has completed the loading?
    // the sender, that's who!
}

You'd have to update each usage of Control_OnTileLoadComplete to use that lamdba. You can change the type from object sender to GMapControl sender if the only 'senders' are of type GMapControl.

Upvotes: 3

Related Questions