Search code examples
c#mouseeventpictureboxmousehover

How can we pop-up picture boxes in c# forms like bing images search do


I am trying to make my dynamic generated picture boxes acts on a mouse hover like the "bing" do. Below is the picture attached of what bing search looks like on mouse hover :

Bing image search mouse hover

Now this is my search results of a project I am working on, what i really want to do is that I want to make picture pop-up as the way which is shown above in bing search.

my project results

Please note that all of the picture boxes are generated dynamically on the run time.


Solution

  • if you are using picture boxes. then you can enhance the current picture box like this and use it.

      //extending the picture box
        public  class PicControl : PictureBox
            {
                // variables to save the picture box old position
                private int _OldWidth;
                private int _OldHeight;
                private int _OldTop;
                private int _OldLeft;
                public PicControl()
                {
                }
        protected override void OnLoadCompleted(System.ComponentModel.AsyncCompletedEventArgs e)
            {
    
                _OldTop = this.Top;
                _OldLeft = this.Left;
                _OldWidth = this.Width;
                _OldHeight = this.Height;
                base.OnLoadCompleted(e);
            }
                protected override void OnMouseEnter(EventArgs e)
              {
                     //once mouse enters 
                      // take the backup of height width top left
                     //so we can restore once mouse leaves
                    _OldTop = this.Top;
                    _OldLeft = this.Left;
                    _OldWidth = this.Width;
                    _OldHeight = this.Height;
                    //decrease the control top left to show hover effect
                    this.Top = this.Top - 10;
                    this.Left = this.Left - 10;
                    // same increase the height width
                    this.Height = this.Height + 20;
                    this.Width = this.Width + 20;
                     // show to control on top of all
                     this.BringToFront();
                    //trigger the base event
                    base.OnMouseEnter(e);
                }
                protected override void OnMouseLeave(EventArgs e)
                {
                       // mouse leaves now we have to restore 
                      //picture box to its original position
                    this.Height = _OldHeight;
                    this.Width = _OldWidth;
                    this.Left = _OldLeft;
                    this.Top = _OldTop;
                    base.OnMouseLeave(e);
                }
            }
    

    Now when you add this class in your project and build it,it will show you PicControl in your toolbox you can replace pictureBox with PicContorl in order get that effect. hope it will helps you. when mouse hover a box enter image description here