开发者

FileDialog DoubleClick Behavior

While developing a WinForms application, I came across what I believe is a bug in the OpenFileDialog and SaveFileDialog controls. A Google search turned up a single other person who noticed the same issue, but neither a solution nor a workaround was provided. You can view this thread at: http://bytes.com/topic/visual-basic-net/answers/389470-open-file-dialog-picturebox-click-event.

I have a custom control on my form that handles the MouseDown event. If I doubleclick a file in a FileDialog control while the mouse is over this control (with the dialog between them, obviously), the MouseDown event gets triggered. I don't think this is an issue with my control, because the person I mentioned before noticed this happening with a PictureBox control. It would seem that even though the mouse button was pressed down (for the second click to open the file) while on the dialog box, the event passed through to the form and my control when the dialog closed.

I have tried disabling my control while the dialog box is active, but that didn't stop it from capturing the event. I assume this is because the eve开发者_如何学Pythonnt is passed down after the dialog closes, so my control would be re-enabled. Does anyone know of a way to prevent that click from reaching the form and, in turn, my control? Also, can anyone confirm if this really is a bug in the FileDialog controls, or if I just have some setting configured incorrectly?


I've heard of this problem before and as far as I know it's a matter of making sure that you handle the Event queue properly. Without seeing your code, it's very difficult to check that your user control implementation is correct but quite often, overriding the mouse events without letting the base events also occur can lead to this sort of behavior.


I was experimenting with the MouseDown and MouseMove events when I realized why my problem was occurring. When the FileDialog box disappeared, the MouseMove event was triggered. In an (admittedly silly) attempt to avoid writing the same block of code twice, I was calling my MouseDown handler from within the MouseMove handler, thinking that certain conditions (namely neither mouse button being held down) would cause the MouseDown handler to effectively do nothing. The issue was that the mouse button was being held down, because the FileDialog box disappears on MouseDown (not MouseClick). This caused the MouseDown handler to execute its conditional code when I wasn't expecting it.

Lesson to learn from this: be painfully careful when chaining event handlers. Or better, pull the common functionality into a method and NEVER chain event handlers. :-)

Thanks to Jelly Amma for giving me the idea to look at the actual events more closely.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜