C # WinForms ShowDialog () call an arbitrary method

I am working on a console application that creates a form to notify users of any given state - at a later stage, the code base will become a class library.

Currently, however, I need to show the form (ShowDialog will be the best method here, I think) THEN call an arbitrary method before closing the form.

As an example, I need to show the form, set the text value of the label control, wait n seconds, then change the label value, and then close the form. I know this sounds a little trivial, but I'm trying to prove the concept of design.

I looked around and it doesn't seem like it is possible since ShowDialog () requires me to close the form before I can continue by listing the code in the calling method / class.

Here is what I still have:

PopUpForm myForm = new PopUpForm(string messageToDisplay); myForm.ShowDialog(); //call myForm.someMethod() here, before the form closes //dispose of the form, now that we've no use for it myform.Dispose(); //target method in PopUpform class public void someMethod() { lblText.Text = "Waiting for some reason"; //wait n number of seconds lblText.Text = "Finished waiting. Form will now close"; //it doesn't matter if the form closes before the user can see this. } 

It appears that ShowDialog () does not support this behavior. I look at BackgroundWorker topics, but wondered if anyone has any advice on this or whether this has occurred before.

+4
source share
3 answers

If you want to show the form, continue to work, and then close it - you can do this via Form.Show() instead of Form.ShowDialog() :

 using (var myForm = new PopUpForm(messageToDisplay)) { myForm.Show(); // Show the form DoWork(); // Do your work... myForm.Close(); // Close it when you're done... } 

However, if this is a purely console application (and it does not have a message pump), then it most likely will not work properly.

Other options: provide a timer in your Form to close it, or pass a delegate to the form to run your method on Show, after which it could close itself.

at a later stage, the code base will become a class library.

When you do this, you probably want to create another mechanism for providing notifications. Combining your library with certain user interface technology is a bad idea. It would probably be better if your library simply provided events or another notification and allowed the user to provide a user interface / notification to the user.

+5
source

If this code, that is, destiend, ends in the code library, I recommend that you do not show it any form of your own free will. The library must generate events or, in some cases, exceptions that may be caught by the calling application to allow it to display the form. If some parts that require presentation to the user are internal to the library, derive the DisplayEventData () method from the library.

+3
source

ShowDialog -method creates a β€œmodal” window and usually locks the user interface until you close it (either by clicking OK or Cancel). You will need to create WinForm yourself, it can be simple and create a message pump for it. You can run your own form by calling

Application.Run(YourForm);

You will need to hold the console stream using the mutex, for example, so that it does not continue and the form is open. The form offers all the methods that you know from WinForms, such as Close , where you could indicate that your console stream will continue.

See this on MSDN.

+2
source

All Articles