question

OktayBozdemir-7731 avatar image
0 Votes"
OktayBozdemir-7731 asked karenpayneoregon edited

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

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.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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.



taskdialog.png (8.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

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



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.