Why do I lose mouse events over a picturebox ?

richardinwpb

Member
Joined
May 19, 2021
Messages
10
Programming Experience
10+
This is regarding a WinForms application written in c# using .Net 4.7.2.

I have a form that has a Top/Down splitter panel. The lower split panel contains Left/Right splitter panel. The Right split panel contains two panels, only one of which is visible at any given time: one contains a flowpanel containing image thumbnails and the other (which I will call the "imagepanel") contains a picturebox with Docking set to full. Right-clicking on a thumbnail hides the flowpanel and displays the imagepanel with the selected thumbnail shown in full in the picturebox. Right-clicking on the PictureBox hides the imagepanel and displays the flowpanel.

When I right-click on a thumbnail for the very first time after running my application, the MouseMove event for the picturebox fires as expected. I have a test label elsewhere that displays the coordinates of the cursor that demonstrates that the MouseMove event is firing. When I right-click on the Picturebox to go back to the flow panel and click on another thumbnail (or the same thumbnail), the MouseMove event never fires again unless I physically left-click on the picturebox.

I have tried setting Focus to the picturebox with PictureBox1.Focus();

I have tried selecting the picturebox with PictureBox1.Select();

I have tried simulating a left mouse-click. I know the simulated mouse click works because when I simulate the left mouse click over a button, it fires the Click event for the button. When I simulate the mouse click over the picturebox, it has no effect.

When I write this.ActiveControl.Name to the console in a variety of places, I always get the name of the Top/Down splitter panel, including the Click event of the PictureBox.

Why am I losing MouseMove events over the picturebox without having to click on it again ? I don't want my users to have to add this extra step.

I have tried creating just a simple application with minimal code to attempt to reproduce the issue, but, of course, I couldn't reproduce the issue.
 
The strangest thing....

Up until now, on the flow panel that contains the thumbnails, I was left-clicking to select an image and right-clicking to zoom the image (by hiding the flow panel and showing the picturebox.

When I switch this around and left-click to zoom the image and right-click to select an image, I no longer have the mousemove issue and everything is working as expected.

Why would this be happening because of the button used? Can this be remediated?
 
The strangest thing....

Up until now, on the flow panel that contains the thumbnails, I was left-clicking to select an image and right-clicking to zoom the image (by hiding the flow panel and showing the picturebox.

When I switch this around and left-click to zoom the image and right-click to select an image, I no longer have the mousemove issue and everything is working as expected.

Why would this be happening because of the button used? Can this be remediated?
I mentioned in my original post that I was simulating a Left Mouse Click, but that it hadn't worked.

When I put everything back the way it used to be...

And I added two consecutive LeftMouseClicks...

It worked!!!

This is the LeftMouseClick code:

C#:
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern bool SetCursorPos(int x, int y);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        public const int MOUSEEVENTF_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF_LEFTUP = 0x04;

        //This simulates a left mouse click
        public static void LeftMouseClick(int xpos, int ypos)
        {
            SetCursorPos(xpos, ypos);
            mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
        }
 
Glad you found a workaround.

Usually the left click also invokes window activation behavior. But in your case the window is already activated so everything should be happy. On the other hand, it's quite possible there is a corner case not covered in WinForms that is hit by you scenario of hiding and showing panels. (Personally, I've found the WinForms FlowLayoutPanel a little quirky, but that was in form re-layout and DPI or system metrics change scenarios.)
 
Back
Top Bottom