Results 1 to 3 of 3

Thread: [1.0/1.1] PictureBox's ImageChanged event

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    [1.0/1.1] PictureBox's ImageChanged event

    Is there any event that's something like this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width