Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal MessageBox in WPF application

Tags:

c#

wpf

messagebox

I'm trying to show a standard MessageBox as a modal window in my application, but it ends up as non modal. In the first call, in the code below I'm showing a standard MessageBox which is shown modal, as it should. In the second call, it isn't shown as modal, even if I grab the main window dispatcher.

Dispatcher disp = Application.Current.MainWindow.Dispatcher;
//First call, shown MODAL
if (this.messageService.ShowYesNo("Do you want to update the Word document, this will regenerate inspectiondata for document", "") == MessageBoxResult.Yes)
{
    using (new WaitCursor())
    {
        _eventAggregator.GetEvent<ProgressBarRequestShow>().Publish("");
        worker = new BackgroundWorker();

        worker.DoWork += delegate(object s, DoWorkEventArgs args)
        {
            AITUpdateProgressDelegate update = new AITUpdateProgressDelegate(UpdateProgress);
            this.docService.UpdateWorddocument(this.docService.GetCurrentDocumentFilePath, update);
        };

        worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
        {
            try
            {
                // Second call NOT MODAL
                disp.Invoke((Action)delegate()
                {
                    this.messageService.ShowInformation("Document generated, choose Open in Word in main toolbar to show document", "");
                });
                _eventAggregator.GetEvent<ProgressBarRequestHide>().Publish("");
            }
            finally
            {
            }
        };
        worker.RunWorkerAsync();
    }
}
like image 688
klashagelqvist Avatar asked May 15 '12 09:05

klashagelqvist


People also ask

How to Show MessageBox in WPF?

Simple MessageBox Clicking on the OK button closes the MessageBox. The following line of code uses the Show method to display a message box with a simple message: MessageBoxResult result = MessageBox. Show("Hello MessageBox");

Is MessageBox show a modal dialog window?

Displays a message window, also known as a dialog box, which presents a message to the user. It is a modal window, blocking other actions in the application until the user closes it. A MessageBox can contain text, buttons, and symbols that inform and instruct the user.

What is modal and modeless dialog box in C#?

Modal dialog boxes, which require the user to respond before continuing the program. Modeless dialog boxes, which stay on the screen and are available for use at any time but permit other user activities.


1 Answers

This looks like what you are looking for. The call for the message box includes an 'owner' parameter. I have used a similar concept in code that i have done before and has showed the windows as modal. Sample code can also be downloaded from the link.

like image 76
Mo Patel Avatar answered Oct 07 '22 19:10

Mo Patel