I work in C # with WinForms in a large application with several forms.
At several points, I have another form that appears as a progress screen. Since I cannot freeze my UI thread, I have to start a new form in a new thread. I use progressform.ShowDialog() to run the form, but since it is in a new thread, you can click or Alt + Tab to return to the main form. I want to disable this.
My thought is that I can put an EventHandler in the mainForm.GotFocus event and redirect the focus to progressForm if it is shown. However, the GotFocus event does not fire when switching applications or moving between progressForm and mainForm . I guess this is because some element in mainForm has focus, not the form itself.
If anyone knows how best to do this (I am not committed to the EventHandler approach) or working code for the EventHandler approach, this will solve my problem.
Edit
According to the comment, I tried to use the Activated event.
// in InitializeForm() this.Activated += FocusHandler; // outside of InitializeForm() void FocusHandler(object sender, EventArgs e) { if (ProgressForm != null) { ProgressForm.Focus(); } }
But he still allowed me to return to the main form and press the buttons.
Chris source share