SkareMeatPackers
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.pictu rebox???
/Lasse
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.pictu
/Lasse
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.Pictu reBox
{
//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();
}
}
}
--------------------
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.Pictu
{
//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_OnImag eChange);
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
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(
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
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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());
}
}
}