Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
i have 2 picturebox each has event Mousedown ,i need to suscripe this event 
so i can call this event like Fun[i]
i think function to pointer could do this, but becuase it has to be in 
Unsafe,i do not like it .My problem is could not convert SampleDelegate to private void 
look at this
            SampleDelegate[] Fun = { EventMouse_0, EventMouse_1 };


What I have tried:

SampleDelegate[] Fun = { EventMouse_0, EventMouse_1 };

for(i=0;i<2;i++)
{
//assume in the next line the mouse has enter to one of the two pictureBox
Fun[i];
}
//private void mymouseDown(object sender, MouseEventArgs e)
privat void EventMouse_0()//mouseDown 
{
messageBox("U are in picturebox_0");
}

privat void EventMouse_1()//mouseDown
{
messageBox("u are in picturebox_1");
}
Posted
Updated 10-Aug-22 6:06am

If you subscribe to a physical event, then when the subscriber is called, it receives a reference to the control that received it. See Control.MouseDown Event (System.Windows.Forms) | Microsoft Docs[^].
 
Share this answer
 
private void FormLoad(object sender, EventArgs e)
     {
         this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pb1MouseDown);
         this.pictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pb2MouseDown);
     }

// i added
MouseEventHandler
for each pictureBox together with a formLoad above this solve my question
no need for the for loop below
for(i=0;i<2;i++)
{
//assume in the next line the mouse has enter to one of the two pictureBox
Fun[i];
}


thank you very much
 
Share this answer
 
Comments
Richard Deeming 11-Aug-22 4:19am    
NB: The new System.Windows.Forms.MouseEventHandler part hasn't been required since C# 1.0; you can simply use:
this.pictureBox1.MouseDown += this.pb1MouseDown;
this.pictureBox2.MouseDown += this.pb2MouseDown;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900