Is there any event that's something like this?
Printable View
Is there any event that's something like this?
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: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.Code: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;
}
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:becomes this:Code:public delegate void ImageChangedHandler(object sender, EventArgs e);
public event ImageChangedHandler ImageChanged;
Code:public event EventHandler ImageChanged;