I'm working in C# with WinForms in a large application with multiple forms.
At several points I have another form coming up as a progress screen. Because I can't freeze my UI thread, I have to start the new form in a new thread. I'm using progressform.ShowDialog()
to start the form, but because it's in a new thread, it is possible to Click or Alt + Tab
back to the main form. I want to disable this.
My thought is that I can put an EventHandler on the mainForm.GotFocus
event and redirect focus to progressForm
if it is shown. However, the GotFocus
event isn't being triggered when you switch applications or move between the progressForm
and mainForm
. I'm guessing that it's because some element in mainForm
has focus, not the form itself.
If anyone knows of a better way to do this (I'm not committed to the EventHandler approach) or working code for the EventHandler approach, it would solve my problem.
Edit
As per the comment, I tried using the Activated
event.
// in InitializeForm()
this.Activated += FocusHandler;
// outside of InitializeForm()
void FocusHandler(object sender, EventArgs e)
{
if (ProgressForm != null)
{
ProgressForm.Focus();
}
}
But it still allowed me to click back to the main form and click buttons.