Mouse left button up event and openfiledialog
Asked Answered
T

2

5

I have few images in a grid, then when i click a button, a "open file dialog" comes up.(of course, over the images)

Microsoft.Win32.OpenFileDialog dlgOpenFiles = new Microsoft.Win32.OpenFileDialog(); dlgOpenFile.DoModal();

The images have a LeftButtonUp event attached. The problem is that if i select a file by double clicking it, the open file dialog closes(which is good), but besides that, the image behind the clicked file is receiving a LeftButtonUp message which is not good at all.

I am using wpf/c#/vs2010

Transversal answered 9/6, 2010 at 10:38 Comment(2)
can you share your layout? Are you sure your image is not being clicked anytime?Heave
I also have the same problem. I would consider this a bug of the microsoft common dialog box. Before showing the dialog box, I removed the event handling function from the event chain using the -= operator, then after the dialog box is closed, I add the event handling function back, and soon after I add them back, they are fired automatically...I just can't get rid of it anyway.Occident
L
5

The simple way to get around it, is whenever you need a handler to button-up event, add a button-down event, do CaptureMouse() in it. Now in your button-up event you can ignore all events, which happen without IsMouseCaptured. And make sure not to forget ReleaseMouseCapture():

private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    CaptureMouse();
}

private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (!IsMouseCaptured)
        return;
    ReleaseMouseCapture();
    var dlg = new OpenFileDialog();
    var res = dlg.ShowDialog(this);
    // ...
}
Lawrencelawrencium answered 9/6, 2010 at 12:7 Comment(0)
R
0

12 years later...

repka's answer (above) didn't work for me in a WPF UserControl implementation, but it got me going down the right path. Same concept, just using a bool variable instead of CaptureMouse(). So far, testing has been positive.

Thanks repka!

Example:

    private bool _mouseDown = false;

    private void LinkButton_LeftMouseButtonDown(object sender, MouseButtonEventArgs e)
    {
        this._mouseDown = true;
    }

    private void LinkButton_LeftMouseButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (!this._mouseDown)
             return;
        this._mouseDown = false;

        //MouseUp logic here
    }
Roundel answered 1/4, 2023 at 2:20 Comment(1)
I used this (before even looking at your answer) but it does not work for me... :/Paramaribo

© 2022 - 2024 — McMap. All rights reserved.