7

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

Arianule
  • 8,811
  • 45
  • 116
  • 174

4 Answers4

12

Just add an event handler using the += operator:

picOneFaceUpA.MouseClick += new MouseEventHandler(your_event_handler);

Or:

picOneFaceUpA.MouseClick += new MouseEventHandler((o, a) => code here);
Jon B
  • 51,025
  • 31
  • 133
  • 161
  • I find my picturebox doesn't have `MouseClick`? – Allan Ruin Nov 13 '14 at 07:13
  • @AllanRuin - It does, as do all (or most) WinForms controls: http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox_events(v=vs.85).aspx – Jon B Nov 13 '14 at 18:07
  • Orz, I found it. Previously I type the picturebox instance in non-contructor part of my class, so MouseClick is not usable. – Allan Ruin Nov 14 '14 at 00:53
  • what if instead of a picturebox i am using simply an image object? – Ashton Oct 18 '16 at 19:20
  • @Ashton An image object isn't a UI control; it's just image data. You'll still need to put it in some kind of control to display it on the UI. – Nyerguds Dec 09 '16 at 10:12
1

Best way in WinForms to subscribe to event - select your control in designer, go to Properties window and select events tab. Then choose any event you want (Click, MouseClick, etc) and double-click on space to the right of event name. Event handler will be generated and subscribed to event.


If you want to subscribe to event manually, then add event handler to event

picOneFaceUpA.MouseClick += PicOneFaceUpA_MouseClick;

Hitting Tab after you write += will generate handler. Or you can write it manually:

void PicOneFaceUpA_MouseClick(object sender, MouseEventArgs e)
{
   // handle click event
   if (e.Button == MouseButtons.Left)
       MessageBox.Show("Left button clicked");
}

BTW if you don't need additional info about click event (e.g. which button was clicked), then use Click event instead. It handles left button clicks by default:

picOneFaceUpA.Click += PicOneFaceUpA_Click;

And handler:

void PicOneFaceUpA_Click(object sender, EventArgs e)
{
   // show another picture box
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • @carny666 changed to MouseClick, but actually if OP is interested only in `picturebox control and upon clicking it `.. then Click should be enough – Sergey Berezovskiy Nov 19 '12 at 14:15
  • Minor comment... You gotta hit tab twice to generate the function. The first tab only generates the text 'PicOneFaceUpA_Click'. – NutellaAddict Nov 13 '15 at 02:22
1

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();
    }
John Koerner
  • 37,428
  • 8
  • 84
  • 134
1

It seems you know how to add dynamic controls then you should just do

this.picOneFaceUpA.MouseClick += new MouseEventHandler(yourMethodName); //hook

and to remove a event

this.picOneFaceUpA.MouseClick -= yourMethodName;     //unhook

the method should be declared something like this:

private void yourMethodName(object sender, MouseEventArgs e)
{
    //your code here
}
Mr_Green
  • 40,727
  • 45
  • 159
  • 271