0

After clicking on a button, I show a progress form and start a task. I want to disable the button or make the progress form a modal so nothing should clickable on the main form, till the progress is completed.

I Tried passing owner reference as an argument when calling the progress bar form's ShowDialog:

m_oProgressBarForm = new ProgressBarForm(); m_oProgressBarForm.ShowDialog(this); 

Please help how can I prevent users to click on button again by graying out the main window or making the buttons not clickable or making the progress bar window as the modal?

Currently the user can click the button again and another instance starts which makes the application not usable, and have to manually kill the application using task manager.

5
  • 2
    You'll probably need to add some more code for context Commented Jul 27, 2016 at 18:39
  • Your progress form looks like it's modal. It's unclear to us why you say it isn't. Commented Jul 27, 2016 at 18:50
  • Yes but its not serving the purpose it should. Can you suggest how to rectify this problem ? Commented Jul 27, 2016 at 18:52
  • It's very unclear how you want us to help you. All you have shown us is two lines of code that doesn't demonstrate the problem. Commented Jul 27, 2016 at 18:58
  • You can not show a waiting/loading form as modal. If you show a form using ShowDialog() the next line of code after the ShowDialog will execute after the dialog closed, while you probably need to show loading and the perform some operations and then close the form. Commented Jul 27, 2016 at 21:09

1 Answer 1

1

If you show a form using ShowDialog() the code following it is not executed until after the dialog box is closed. But you probably need to show progress-bar form and then perform some operations and then close the form, You can not show your progress form as modal.

Instead you can disable your main form and the show your progress form by setting the main form as its parent, then perform the time-consuming task and at last, close the progress form and enable the main form:

private async void Button1_Click(object sender, EventArgs e) { var f = new Form(); //Your progress form f.Show(this); this.Enabled = false; try { //For example a time-consuming task await Task.Delay(5000); } catch (Exception ex) { //Handle probable exceltions } f.Close(); this.Enabled = true; this.BringToFront(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.