Click here to Skip to main content
15,887,464 members
Home / Discussions / C#
   

C#

 
QuestionHow to register a new List<IChecker> to Actofac ContainerBuilder Pin
Johan Bertilsdotter19-Apr-12 21:25
Johan Bertilsdotter19-Apr-12 21:25 
AnswerRe: How to register a new List to Actofac ContainerBuilder Pin
Dave Kreskowiak20-Apr-12 3:30
mveDave Kreskowiak20-Apr-12 3:30 
Questioncreating event handler for picturebox click event Pin
Member 857219719-Apr-12 13:10
Member 857219719-Apr-12 13:10 
AnswerRe: creating event handler for picturebox click event Pin
DaveyM6919-Apr-12 14:47
professionalDaveyM6919-Apr-12 14:47 
QuestionRe: creating event handler for picturebox click event Pin
Member 857219720-Apr-12 4:32
Member 857219720-Apr-12 4:32 
QuestionRe: creating event handler for picturebox click event Pin
DaveyM6920-Apr-12 6:23
professionalDaveyM6920-Apr-12 6:23 
AnswerRe: creating event handler for picturebox click event Pin
Member 857219720-Apr-12 6:36
Member 857219720-Apr-12 6:36 
AnswerRe: creating event handler for picturebox click event Pin
DaveyM6920-Apr-12 7:05
professionalDaveyM6920-Apr-12 7:05 
No, that isn't possible - but it doesn't sound like a good way to me anyway. The += needs a EventHandler delegate instance as a parameter. This can be done by using either an existing instance, a new instance or just a method name with the correct signature.

Have you considered an alternative aproach - and using one event handler for all the picture boxes?
You can attach the event handler to all PictureBoxes in a simple loop:
C#
foreach (Control control in this.Controls)
    if (control is PictureBox)
        control.Click += PictureBoxClickHandler;

and your handling method would start like this:
C#
private void PictureBoxClickHandler(object sender, EventArgs e)
{
    PictureBox pictureBox = sender as PictureBox;
    if (pictureBox != null)
    {
        // pictureBox is the PictureBox that was clicked.
        // Do your stuff
    }
}

You can now use any property (perhaps Name as you want to use strings, the Tag property is designed for this though) to identify the picture box in question and react as you wish. I usually prefer to subclass the control and add an Id property:
C#
public class MyPictureBox : PictureBox
{
    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }
}

C#
foreach (Control control in this.Controls)
    if (control is MyPictureBox)
        control.Click += MyPictureBoxClickHandler;

C#
private void MyPictureBoxClickHandler(object sender, EventArgs e)
{
    MyPictureBox myPictureBox = sender as MyPictureBox;
    if (myPictureBox != null)
    {
        switch (myPictureBox.Id)
        {
            case 1:
                // do stuff for 1
                break;
            case 2:
            case 3:
                // do stuff for 2 and 3
                break;
            default:
                // default id (0)
                break;
        }
    }
}

[Edit] You would obviously need to use MyPictureBoxes instead of the built in PictureBox. After a build, it will show in your toolbox for drag and drop - or you can just change the code where they are created.
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: creating event handler for picturebox click event Pin
Member 857219720-Apr-12 7:11
Member 857219720-Apr-12 7:11 
QuestionDodger Game in C# Pin
Qobacha19-Apr-12 9:26
Qobacha19-Apr-12 9:26 
AnswerRe: Dodger Game in C# Pin
Eddy Vluggen19-Apr-12 9:49
professionalEddy Vluggen19-Apr-12 9:49 
GeneralRe: Dodger Game in C# Pin
PIEBALDconsult19-Apr-12 12:00
mvePIEBALDconsult19-Apr-12 12:00 
GeneralRe: Dodger Game in C# Pin
Qobacha19-Apr-12 20:04
Qobacha19-Apr-12 20:04 
QuestionRe: Dodger Game in C# Pin
Eddy Vluggen20-Apr-12 0:43
professionalEddy Vluggen20-Apr-12 0:43 
GeneralRe: Dodger Game in C# Pin
Qobacha23-Apr-12 10:06
Qobacha23-Apr-12 10:06 
AnswerRe: Dodger Game in C# Pin
Eddy Vluggen23-Apr-12 11:09
professionalEddy Vluggen23-Apr-12 11:09 
GeneralRe: Dodger Game in C# Pin
Qobacha24-Apr-12 2:41
Qobacha24-Apr-12 2:41 
AnswerRe: Dodger Game in C# Pin
Eddy Vluggen24-Apr-12 4:55
professionalEddy Vluggen24-Apr-12 4:55 
GeneralRe: Dodger Game in C# Pin
Qobacha26-Apr-12 9:44
Qobacha26-Apr-12 9:44 
AnswerRe: Dodger Game in C# Pin
Eddy Vluggen26-Apr-12 9:54
professionalEddy Vluggen26-Apr-12 9:54 
GeneralRe: Dodger Game in C# Pin
Qobacha27-Apr-12 7:48
Qobacha27-Apr-12 7:48 
GeneralRe: Dodger Game in C# Pin
Eddy Vluggen27-Apr-12 7:57
professionalEddy Vluggen27-Apr-12 7:57 
GeneralRe: Dodger Game in C# Pin
Qobacha26-Apr-12 13:17
Qobacha26-Apr-12 13:17 
GeneralRe: Dodger Game in C# Pin
Eddy Vluggen27-Apr-12 7:54
professionalEddy Vluggen27-Apr-12 7:54 
GeneralRe: Dodger Game in C# Pin
Qobacha27-Apr-12 9:26
Qobacha27-Apr-12 9:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.