2 Replies - 2448 Views - Last Post: 22 March 2011 - 09:41 AM Rate Topic: -----

#1 hookiethe1  Icon User is offline

  • D.I.C Lover

Reputation: 416
  • View blog
  • Posts: 1,335
  • Joined: 28-September 10

Passing additional arguments to system event handler

Posted 22 March 2011 - 09:16 AM

Hey folks, I'm working on an assignment for school. I have an array of 64 pictureboxes (and immediately those of you who have been around the block know I'm making a chess game!). I am using a for loop to set all of the necessary properties and event handlers for the pictureboxes as such:

for (int i = 0; i < 64; i++)
{
//set some properties
PicBoxList[i].MouseDown += new MouseEventHandler(PicBox_MouseDown);
}

void PicBox_MouseDown(object sender, MouseEventArgs e)
{
//in here I need to reference PicBoxList[i], so I need to pass the picturebox itself, or the //array and index in here somehow
}




I've been reading a lot about delegates and such, but it's a little confusing and I'm not sure if I need a delegate here because from what I understand, they are for using multiple methods for the same eventhandler? Is there a simpler way to add an argument to an event handler?

Thanks for help!

Is This A Good Question/Topic? 0
  • +

Replies To: Passing additional arguments to system event handler

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Passing additional arguments to system event handler

Posted 22 March 2011 - 09:31 AM

new MouseEventHandler(PicBox_MouseDown);


That part there is a delegate.

You can't change the parameters of an event handler, because they have to match the type of delegate for that event. In this case, it's a MouseEventHandler delegate, which demands and object and a MouseEventArgs (hint: all events will take an object and some kind of event argument).

However, you already have a reference to the picturebox that triggered the event. Why do you think it is named sender? It's an object, so you'll have to cast it as a PictureBox, but that's what it is. It's the object that sent the event.

This post has been edited by insertAlias: 22 March 2011 - 09:32 AM

Was This Post Helpful? 3
  • +
  • -

#3 hookiethe1  Icon User is offline

  • D.I.C Lover

Reputation: 416
  • View blog
  • Posts: 1,335
  • Joined: 28-September 10

Re: Passing additional arguments to system event handler

Posted 22 March 2011 - 09:41 AM

Ah, I thought that, and tried using sender but it didn't work, didn't realize I had to cast it. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1