How do I block all keyboard and mouse input in my WinForms application?

I have a WinForms application that fetches data from a web service into a worker thread and I need to disable all input for my application until the data is loaded.

I am currently creating a semi-transparent form and placing it on top of my application. When the data call completes, I close this overlay form. This works great, causing significant performance issues for users running the application through Terminal Services. I tried to make the overlay completely transparent, but that still causes two redraws of the entire window, so it didn't help.

I know the general recommendation for handling this is to disable all controls, but this will also heavily redraw the screen, so I'm looking for another way to block all user input. Any help would be greatly appreciated!

UPDATE: I should have mentioned that we covered a modal dialog. We are currently showing the overlay, starting the data access flow, then creating the form. If there isn't a better way to block input (App.BlockInput () might be nice) then we could use the concept of a modal dialog, but we'll have to wait until the form construct is complete and there is currently no good central location for that.

+2


source to share


5 answers


You can display a small modal form ( modalForm.ShowDialog(yourForm)

) with a progress bar collapsed on top of your application. This will not redraw large areas.



+4


source


If your application is indeed blocked during an operation, I would do what Microsoft often does: open a modal dialog box with some kind of Tibetan animation or ProgressBar and a Cancel button. Redraw is limited because you only paint the size of the new dialog, and entry to the rest of your application is blocked because the dialog is modal. In addition, users are much more willing to wait when you have some status updates and animations, because it looks like the computer is "working".

However, if there are actions your user can take while executing a web service request, it is best to leave the controls available. At the very least, there should always be a way to abort / abort a process.



Refresh . Since you have now changed the question: how long does it take to create a modal dialog? Why not just build the dialog empty and then populate its controls? If you have a small dialog box with one button and one ProgressBar, then calling dialog.ShowDialog () should be faster than your user can interact with your UI. Isn't that so?

+3


source


Using Application.AddMessageFilter can do this: Capture mouse events from each component in C # WInForm

+3


source


One thing you might try for keyboard input is setting the form's KeyPreview property to True. This will first pass all keyboard events to the Form object and not to the individual controls. Create an event handler for the KeyPress event of the form, and there you can set the Handled property for KeyPressEventArgs to True to prevent the key step from navigating to any of the controls. If you are currently fetching data from a web service, set the Handled property to True, otherwise set it to False and the keystroke will be passed to the controls.

If anyone has a good idea on how to handle mouse input but you are in the mood.

+2


source


I usually create LockUI () and UnlockUI () functions on my form that toggle controls and flip the local form field, which acts as a flag indicating a long running process. This approach works really well if you are using the command pattern.

As mentioned earlier, you can switch keyboard input using the KeyPreview property of the form (as suggested by TLiebe).

As far as mouse input is concerned, you can disable mouse activity by plugging in WinProc messages and intercepting mouse input messages . This is basically what KeyPreview does.

0


source







All Articles