Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stop users from pressing other buttons until code finishes executing? 1

Status
Not open for further replies.

dalchri

Programmer
Joined
Apr 19, 2002
Messages
608
Location
US
I've got a preview button in my app that runs a crystal report. For the first few seconds all is well. You can't click anything on the form and the cursor is an hourglass (as set by my code). But then the form becomes responsive again, the hourglass turns back into a pointer, and the user can select File, Close for example.

So for example, the user can open an order, print it, and close the order before the crystal report code finishes printing, causing an error.

I've tried this.Enabled = false; This does work but it makes all the controls greyed out. Its not that I want to prevent form events from firing as much as I want to ignore user mouse and keyboard input.

Is there a simple way to do this?
 
If the form is not responding and you set the cursor to HourGlass, the events are still fired. They just wait in the message queue until the form response.
If you set Enabled = false, the control still process events, except from those being received from the user such as mouse or keyboard.
If you want to disable some features of the form, make Enabled = false only for the controls that are responsible to those features.
If you want to leave all controls not grayed but still not processing events, there are some ways:
1. Disconnect all event handlers when the report is processing, like:
Code:
		private void DoReport()
		{
			this.button1.Click -= new System.EventHandler(this.button1_Click);
			this.menuItem1.Select -= new System.EventHandler(this.menuItem1_Select);

			// Do report

			this.button1.Click += new System.EventHandler(this.button1_Click);
			this.menuItem1.Select += new System.EventHandler(this.menuItem1_Select);
		}
2. Use controls that looks normally even if Enabled = false. I don't know how to do it with Microsoft controls, but there are third party control for that.

3. Make a class level variable that DoReport method will set to true. In the event handlers that you want not to be performed when the report is being processed, check if this variable is true or false.

I prefer the second method because in the others the user clicks some buttons but doesn't understand why nothing works.
 
Since I work with Windows Forms so much I forget that they are inherited and that I have access to overriding the protected methods. I had another idea that I wanted to see if anyone could flesh out.

If I override WndProc, does anyone know if there is an easily and reliably defined set of windows messages that I can ignore (not pass onto the base class) without getting the form into a messy state?

Are there any other protected methods that I could block in the same way to prevent the mouse and keyboard input being sent to the child controls?

Is PreProcessMessage used for this purpose?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top