PictureBox MouseEnter / MouseLeave events are not fired

Create a new form with three drawers. This code is designed to draw a border when the mouse enters the image field and removes it when it leaves. This is not consistent with the results. Sometimes it draws / removes the border, sometimes it is not. This code is not complicated. Using VS 2012.

Private Sub PictureBox_MouseEnter(sender As Object, e As EventArgs) _ Handles PictureBox1.MouseEnter, PictureBox2.MouseEnter, PictureBox3.MouseEnter Dim pb As PictureBox = DirectCast(sender, PictureBox) pb.BorderStyle = BorderStyle.FixedSingle ' Debug.WriteLine("E " & pb.Name) End Sub Private Sub PictureBox_MouseLeave(sender As Object, e As EventArgs) _ Handles PictureBox1.MouseLeave, PictureBox2.MouseLeave, PictureBox3.MouseLeave Dim pb As PictureBox = DirectCast(sender, PictureBox) pb.BorderStyle = BorderStyle.None ' Debug.WriteLine("X " & pb.Name) End Sub 
+6
source share
3 answers

I could also reproduce this problem. Therefore, adding the comments above about “drawing something else” instead of using the “Picturebox” property, let me suggest this quick and dirty approach:

  • Use the RectangleShape object provided by VisualBasic Powerpack 3.0 . Simply put, one of those in the same form is in your PictureBox and makes it invisible (visible = false).

  • The code is also simple:

     Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.RectangleShape1.Location = New Point(Me.PictureBox1.Left - 1, Me.PictureBox1.Top - 1) Me.RectangleShape1.Size = New Size(Me.PictureBox1.Width + 1, Me.PictureBox1.Height + 1) End Sub Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter Me.RectangleShape1.Visible = True End Sub Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave Me.RectangleShape1.Visible = False End Sub End Class 
+1
source

You need help with the Form MouseEnter event.

 Dim pb As PictureBox = New PictureBox Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter pb.BorderStyle = BorderStyle.None End Sub Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove pb = PictureBox1 pb.BorderStyle = BorderStyle.FixedSingle End Sub 
0
source

I followed the idea of ​​KalaNag by putting my image box inside the panel and handling the event in the pciturebox folder, doing this

 private void PictureBox_MouseEnter(object sender, EventArgs e) { PictureBox control = sender as PictureBox; (control.Parent as Panel).Width = 20; (control.Parent as Panel).Height = 20; (control.Parent as Panel).BorderStyle = BorderStyle.Fixed3D; } private void PictureBox_MouseLeave(object sender, EventArgs e) { PictureBox control = sender as PictureBox; (control.Parent as Panel).Width = 18; (control.Parent as Panel).Height = 18; (control.Parent as Panel).BorderStyle = BorderStyle.None; } 

I changed the size of the control, because otherwise the picture continues to flicker when the mouse draws borders, when the cursor enters and leaves endlessly, since the borders change the size of the control.

It works like a charm!

0
source

Source: https://habr.com/ru/post/946725/


All Articles