question

Nedudale-0390 avatar image
0 Votes"
Nedudale-0390 asked JackJJun-MSFT answered

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

 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.

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@Nedudale-0390, 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.


5.gif (318.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.