How To Open The MessageBox Window In The Middle Of The Application Window?

Oktay Bozdemir 21 Reputation points
2021-03-27T19:02:25.93+00:00

Hi all,

I wrote a C # windows application application. I want to open the MessageBox alert in the middle of the application? How can I do it?

Thanks.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,219 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2021-03-29T11:57:51.163+00:00

    I have a code sample here which uses the following NuGet package, WindowsAPICodePack-Core which is free. No perfect but close.

    Example

    82444-taskdialog.png

    using System;  
    using System.Windows.Forms;  
    using DialogsSharp.Classes;  
      
    namespace DialogsSharp  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void CenterOnFormButton_Click(object sender, EventArgs e)  
            {  
                DialogHelpers.CenterOnParent();  
            }  
        }  
    }  
      
    

    Code

    using System;  
    using System.IO;  
    using System.Windows.Forms;  
    using Microsoft.WindowsAPICodePack.Dialogs;  
      
    namespace DialogsSharp.Classes  
    {  
      
        public static class DialogHelpers  
        {  
            public static void CenterOnParent()  
            {  
                var yesButton = new TaskDialogButton("CloseTaskDialogButton", "Yes")  
                {  
                    Default = true  
                };  
                var noButton = new TaskDialogButton("ProceedTaskDialogButton", "No");  
      
                var dialog = new TaskDialog  
                {  
                    Caption = "Question",  
                    InstructionText = $"Would you like to continue?",  
                    Icon = TaskDialogStandardIcon.Information,  
                    Cancelable = false,  
                    StartupLocation = TaskDialogStartupLocation.CenterOwner  
                };  
      
                dialog.Controls.Add(yesButton);  
                dialog.Controls.Add(noButton);  
      
                dialog.OwnerWindowHandle = Form.ActiveForm.Handle;  
      
                dialog.Opened += (senderObject, ea) =>  
                {  
                    var taskDialog = senderObject as TaskDialog;  
                    taskDialog.Icon = taskDialog.Icon;  
                };  
      
                yesButton.Click += (e, a) =>  
                {  
                    Console.WriteLine("Yes");  
                    dialog.Close(TaskDialogResult.Close);  
                };  
      
                noButton.Click += (e, a) =>  
                {  
                    Console.WriteLine("No");  
                    dialog.Close(TaskDialogResult.Close);  
                };  
      
                dialog.Show();  
      
            }  
      
        }  
    }  
      
    

    Edit

    Added another option in the same repository, see this library. I still favor the other library as it has more options overall.


2 additional answers

Sort by: Most helpful
  1. Castorix31 81,481 Reputation points
    2021-03-27T19:13:22.97+00:00

    A way is with a CBT Hook, like at : How to Make MessageBoxes Center on their Parent Forms

    0 comments No comments

  2. Timon Yang-MSFT 9,571 Reputation points
    2021-03-29T07:23:36.393+00:00

    You can also customize a Form and use it as message box.

            public MyMessageBox(string message, string title)  
            {  
                InitializeComponent();  
                this.StartPosition = FormStartPosition.CenterScreen;// Or wherever   
      
                label1.Text = "\r\n" + message;  
                this.Text = title;  
                this.Location = ...  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                this.Close();  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments