How can I detect if mouse i hover picturebox control and then to disply or not the picturebox ?

Nedudale 141 Reputation points
2021-10-24T22:41:45.347+00:00
private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            PictureBox pb = new PictureBox();

            pb.Size = new Size(512, 512);
            pb.Left = (this.ClientSize.Width - pb.Width) / 2;
            pb.Top = (this.ClientSize.Height - pb.Height) / 2;
            pb.BringToFront();

            this.Controls.Add(pb);

            pb.Image = new Bitmap(files[0]);
        }

When the mouse cursor is hover inside pictureBox1 area it's creating and showing the new pb pictureBox control but I want now that if I move the mouse cursor outside the pictureBox1 area then don't display the new pb and over again display pb only when the mouse is inside the pictureBox1 area.

Now when I put the mouse cursor inside pictureBox1 area it will show pb forever no matter if the mouse cursor is inside anymore or not.

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-10-25T02:06:30.987+00:00

    @Nedudale , you could try to use Mouse_Leave event to get what you want.

    Here is a code example you could refer to.

    private void Form1_Load(object sender, EventArgs e)  
            {  
                string path = Path.Combine("D:\\", "1.PNG");  
                pictureBox1.Image = Image.FromFile(path);  
      
            }  
      
            private void pictureBox1_MouseHover(object sender, EventArgs e)  
            {  
                PictureBox pb = new PictureBox();  
                pb.Name = "pic1";  
                pb.SizeMode = PictureBoxSizeMode.StretchImage;  
                pb.Size = new Size(512, 512);  
                pb.Left = (this.ClientSize.Width - pb.Width) / 2;  
                pb.Top = (this.ClientSize.Height - pb.Height) / 2;  
                pb.BringToFront();  
                string path = Path.Combine("D:\\", "2.jpg");  
                pb.Image = new Bitmap(path);  
                this.Controls.Add(pb);  
                  
                
            }  
      
            private void pictureBox1_MouseLeave(object sender, EventArgs e)  
            {  
                foreach (Control item in this.Controls)  
                {  
                    if(item.Name=="pic1")  
                    {  
                        item.Visible = false;  
                    }  
                }  
            }  
    

    Result:

    143242-5.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.