Results 1 to 5 of 5

Thread: show modal dialog while running code in main form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    Question show modal dialog while running code in main form

    hi all, i was wondering if there is any way to show a modal dialog box, but run code while the owner form is blocked. Setting the main form.Enabled = false to block it redraws all the controls as grayed and disabled, which i do not need. The following pseudo-code is about what would be nice:

    Code:
    private void WorkButton_Click(object sender, System.EventArgs e) {
    	ProcessingForm processing = new ProcessingForm();
    	processing.ShowDialogAsync(this);
    
    	for(int i = 0; i < 1000; i++) {
    		// do stuff
    	}
    	
    	processing.Close();
    }
    Thanks in advance for any help!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: show modal dialog while running code in main form

    You should show the dialogue in the UI because it is a UI element. You can start a new thread or use a delegate to make an asynchronous method call to perform your other task before displaying the dialogue. If you show the dialogue in a different thread then it will not behave as a modal dialogue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    Re: show modal dialog while running code in main form

    i have a multi-threaded setup working, but the app is simple, and i'll be blocking the main interface while it is processing, so going to multithreaded seems a bit overkill. what i really need is a way to disable the main form but still draw it as if it's enabled.

    Code:
    private void WorkButton_Click(object sender, System.EventArgs e) {
    	ProcessingForm processing = new ProcessingForm();
    
    	this.Enabled = false; // except don't gray out controls on this form
    	processing.Show();
    
    	for(int i = 0; i < 1000; i++) {
    		// do stuff
    	}
    	
    	processing.Close();
    	this.Enabled = true;
    }

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    Re: show modal dialog while running code in main form

    ** bump **

    any ideas?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: show modal dialog while running code in main form

    If you want the main form to be refreshed as needed then multithreading is the way to go. Otherwise the only way to refresh it will be for you to call Refresh or DoEvents at intervals through your code. If you want the mani form disabled then you can popup a modal dialogue from the main form and then start the worker thread from that. It will block access to the main form while the thread is executing and it can show progress or whatever if desired.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width