Click to See Complete Forum and Search --> : [1.0/1.1] PictureBox's ImageChanged event
nebulom
Jun 27th, 2006, 09:47 PM
Is there any event that's something like this?
jmcilhinney
Jun 27th, 2006, 10:41 PM
No there isn't, but there's no reason that you can't add one of your own. I've never added my own events in C# and I know it's a little different to VB so I decided to whip up a demo so I could get the feel myself:class ExtendedPictureBox : System.Windows.Forms.PictureBox
{
// The Image property is not virtual so you need to declare a new implementation.
public new System.Drawing.Image Image
{
get
{
return base.Image;
}
set
{
bool imageChanged = base.Image != value;
base.Image = value;
if (imageChanged)
{
// The Image has changed so raise the appropriate event.
this.OnImageChanged(new EventArgs());
}
}
}
protected virtual void OnImageChanged(EventArgs e)
{
// Raise the ImageChanged event.
ImageChanged(this, e);
}
public delegate void ImageChangedHandler(object sender, EventArgs e);
public event ImageChangedHandler ImageChanged;
}
Note that this will work for .NET 1.x but you'd have to do a little more work in 2.0 because of the Load method, which does not set the Image property at any point, even though the object that it would return would have changed.
jmcilhinney
Jul 7th, 2006, 01:15 AM
Just realised too that as the event handler signature is the same as the EventHandler delegate you can just use that instead of declaring your own delegate, i.e. this: public delegate void ImageChangedHandler(object sender, EventArgs e);
public event ImageChangedHandler ImageChanged;becomes this: public event EventHandler ImageChanged;
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.