c# picturebox array how to create handler mouseover for element code example

Example 1: c# picturebox array how to create handler mouseover for element

//You need simply write

pbs[i].MouseEnter += globalMouseEnterEvent;
//of course you need to have a globalMouseEnterEvent somewhere in your code

public void globalMouseEnterEvent(object sender, System.EventArgs e)
{
    ....
}
//However, another piece of information is needed when your work with event shared between numerous controls. You need to recognize the control that triggers the event. The control instance is passed using the sender parameter that you can cast to your appropriate control type, but what is needed is to give a unique identifier to your control. Like setting the Tag or Name properties when you build the control

for (int i = 0; i < pbs.Length; i++)
{
  .....
  pbs[i].Tag = "PB" + i.ToString()
  ...
}
//so in the MouseEnter code you could write

public void globalMouseEnterEvent(object sender, System.EventArgs e)
{
    PictureBox p = sender as PictureBox;
    if(p.Tag.ToString() == "PB1")
        .....
    else if ......
}

Example 2: c# picturebox array how to create handler mouseover for element

//You need simply write

pbs[i].MouseEnter += globalMouseEnterEvent;
//of course you need to have a globalMouseEnterEvent somewhere in your code

public void globalMouseEnterEvent(object sender, System.EventArgs e)
{
    ....
}

Tags:

Misc Example