Link to home
Start Free TrialLog in
Avatar of SkareMeatPackers
SkareMeatPackersFlag for Denmark

asked on

Adding Imagechanged Event to Picturebox Control

Hello.

In C#, i was wondering if it is possible to extend the picturebox control with an event that would fire when the image property has changed?

I know how to do it, by building a new picturebox control, and adding the event here, but can it be done on System.Windows.Forms.picturebox???

/Lasse
Avatar of Babycorn-Starfish
Babycorn-Starfish
Flag of United States of America image

Hi,

you can inherit from PictureBox and just add the events to your subclass:

        //Image changed delegate
        public delegate void ImageChanged(Object sender, EventArgs e);

        //event raised when image changed
        public event ImageChanged OnImageChange;

         //overridden/hidden Image property with event hooked up
        public new Image Image
        {
            get { return base.Image;}
            set
            {
                base.Image = value;
                if (OnImageChange != null)
                {
                    OnImageChange(this, new EventArgs());
                }
            }
        }
Avatar of SkareMeatPackers

ASKER

That was also somewhat my first idea.

I'm trying with this piece of code, but the "OnImageChange" event is not avalible for my picturebox. What am i doing wrong?

Here my code :

------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication13
{

    public class Picturebox : System.Windows.Forms.PictureBox
    {

        //Image changed delegate
        public delegate void ImageChanged ( Object sender, EventArgs e );

        //event raised when image changed
        public event ImageChanged OnImageChange;

        //overridden/hidden Image property with event hooked up
        public new Image Image
        {
            get { return base.Image; }
            set
            {
                base.Image = value;
                if (OnImageChange != null)
                {
                    OnImageChange(this, new EventArgs());
                }
            }
        }

    }

    public partial class Form1 : Form
    {
        public Form1 ( )
        {
            InitializeComponent();
            PictureBox _PB = new PictureBox();
        }
    }
}

--------------------
Hi,


you just need to subscribe to the event. So after your line:

  PictureBox _PB = new PictureBox();

add somethning like this

_PB.OnImageChange += new MyPictureBox.ImageChanged(_PB_OnImageChange);

after you press the = in the += the intellisense will kick and create the handler for the event if you press tab.

Also, i created mine be adding a User Control to the solution, that way you get support to drag it on to the form in the designer.

hth
The problem is, that the "OnImageChange" event does not appear in the list, when i press "." after _pb

So it seems that "OnImageChange" is not avalible. On from the PictureBox class, here i can find the "OnImageChange" event. But not from "public partial class Form1 : Form"


/Lasse
ASKER CERTIFIED SOLUTION
Avatar of Babycorn-Starfish
Babycorn-Starfish
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh, yes. Now it's working.

I just thought that the event could be added to the picturebox control that can be choosen as standard from the toolbox in the designer. Well, i just have to make a usercontrol like you, and use that instead then...

Thnx