|
-
Jun 27th, 2006, 09:47 PM
#1
Thread Starter
Fanatic Member
[1.0/1.1] PictureBox's ImageChanged event
Is there any event that's something like this?
-
Jun 27th, 2006, 10:41 PM
#2
Re: [1.0/1.1] PictureBox's ImageChanged event
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:
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;
}
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.
-
Jul 7th, 2006, 01:15 AM
#3
Re: [1.0/1.1] PictureBox's ImageChanged event
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:
Code:
public delegate void ImageChangedHandler(object sender, EventArgs e);
public event ImageChangedHandler ImageChanged;
becomes this:
Code:
public event EventHandler ImageChanged;
Last edited by jmcilhinney; Jul 7th, 2006 at 01:23 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|