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

phm
  • 1,160
  • 1
  • 17
  • 29
  • can you share your layout? Are you sure your image is not being clicked anytime? – Amsakanna Jun 09 '10 at 11:40
  • 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. – LazNiko Aug 24 '11 at 17:42

2 Answers2

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);
    // ...
}
repka
  • 2,909
  • 23
  • 26
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
    }
Jim A
  • 1
  • 1