views:

54

answers:

1

Using the code below:

Private Sub ShowDropDown()
    Using f As New DropDownForm
        f.Visible = True
        Do While f.Visible
            Application.DoEvents()
            // Call to not take up 100% resources
        Loop
    End Using
End Sub

If the ShowDropDown method is called by anything other than a button click, then the first mouse click in the DropDownForm is ignored. So, if it was called following a PictureBox click, or a Form click, then it's ignored.

I can fix the problem by doing the below:

Private Sub ShowDropDown()
    Using f As New DropDownForm
        f.Visible = True
        Dim capture As IntPtr = GetCapture()
        If (capture <> IntPtr.Zero) Then
            SendMessage(New HandleRef(Nothing, capture), &H1F, IntPtr.Zero, IntPtr.Zero)
            ReleaseCapture()
        End If
        Do While f.Visible
            Application.DoEvents()
        Loop
    End Using
End Sub

This was a guess, after looking at the Form.ShowDialog method in reflector.

My question is, is there a managed call I can make to acheive the same result, and what does a button click do that other clicks don't?

ETA: The problem does not occur if I open the form using a key.

+2  A: 

Yes, mouse capture is your problem. You can fix it by explicitly setting the Control.Capture property to false. For example:

    private void pictureBox1_Click(object sender, EventArgs e) {
        pictureBox1.Capture = false;
        ShowDropDown();
    }
Hans Passant
Thanks for that, I suspected there would be a simpler way.
Jules