Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a mouse click eventhandler to a PictureBox

Tags:

c#

winforms

I have a picturebox control and upon clicking it another PictureBox appears at a specific location. I.o.w it wasnt added from the toolbox.

PictureBox picOneFaceUpA = new PictureBox();
        picOneFaceUpA.Location = new Point(42, 202);
        picOneFaceUpA.Width = 90;
        picOneFaceUpA.Height = 120;
        picOneFaceUpA.Image = Image.FromFile("../../Resources/" + picFaceUpToBeMoved[0] + ".png");
        Controls.Add(picOneFaceUpA);
        picOneFaceUpA.BringToFront();

How would I add a MouseClick eventhandler to this control?

Thanks

like image 523
Arianule Avatar asked Nov 19 '12 14:11

Arianule


People also ask

How do you move an image in a Picturebox?

To move the image, click the move button, click on the image, keep hold the clicked mouse button and drag.

Which control allow a user to trigger an event by using a mouse click?

Explanation: Mouse, keyboard, joystick control works when the user clicks it.


2 Answers

Just add an event handler using the += operator:

picOneFaceUpA.MouseClick += new MouseEventHandler(your_event_handler);

Or:

picOneFaceUpA.MouseClick += new MouseEventHandler((o, a) => code here);
like image 143
Jon B Avatar answered Sep 28 '22 12:09

Jon B


If you type picOneFaceUpA.Click += then hit tab, it will autocomplete for you and implement the event handler:

    private void button2_Click(object sender, EventArgs e)
    {
        PictureBox picOneFaceUpA= new PictureBox();
        picOneFaceUpA.Click += new EventHandler(picOneFaceUpA_Click);
    }

    void picOneFaceUpA_Click(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
like image 45
John Koerner Avatar answered Sep 28 '22 11:09

John Koerner