Show a MessageBox centered in form

22,077

Solution 1

The solution for VB.NET:

This code is taken and translated from an asnwer of @Hans Passant: Winforms-How can I make MessageBox appear centered on MainForm?

Centered_MessageBox.vb

Imports System.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Class Centered_MessageBox
    Implements IDisposable
    Private mTries As Integer = 0
    Private mOwner As Form

    Public Sub New(owner As Form)
        mOwner = owner
        owner.BeginInvoke(New MethodInvoker(AddressOf findDialog))
    End Sub

    Private Sub findDialog()
        ' Enumerate windows to find the message box
        If mTries < 0 Then
            Return
        End If
        Dim callback As New EnumThreadWndProc(AddressOf checkWindow)
        If EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero) Then
            If System.Threading.Interlocked.Increment(mTries) < 10 Then
                mOwner.BeginInvoke(New MethodInvoker(AddressOf findDialog))
            End If
        End If
    End Sub
    Private Function checkWindow(hWnd As IntPtr, lp As IntPtr) As Boolean
        ' Checks if <hWnd> is a dialog
        Dim sb As New StringBuilder(260)
        GetClassName(hWnd, sb, sb.Capacity)
        If sb.ToString() <> "#32770" Then
            Return True
        End If
        ' Got it
        Dim frmRect As New Rectangle(mOwner.Location, mOwner.Size)
        Dim dlgRect As RECT
        GetWindowRect(hWnd, dlgRect)
        MoveWindow(hWnd, frmRect.Left + (frmRect.Width - dlgRect.Right + dlgRect.Left) \ 2, frmRect.Top + (frmRect.Height - dlgRect.Bottom + dlgRect.Top) \ 2, dlgRect.Right - dlgRect.Left, dlgRect.Bottom - dlgRect.Top, True)
        Return False
    End Function
    Public Sub Dispose() Implements IDisposable.Dispose
        mTries = -1
    End Sub

    ' P/Invoke declarations
    Private Delegate Function EnumThreadWndProc(hWnd As IntPtr, lp As IntPtr) As Boolean
    <DllImport("user32.dll")> _
    Private Shared Function EnumThreadWindows(tid As Integer, callback As EnumThreadWndProc, lp As IntPtr) As Boolean
    End Function
    <DllImport("kernel32.dll")> _
    Private Shared Function GetCurrentThreadId() As Integer
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function GetClassName(hWnd As IntPtr, buffer As StringBuilder, buflen As Integer) As Integer
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef rc As RECT) As Boolean
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function MoveWindow(hWnd As IntPtr, x As Integer, y As Integer, w As Integer, h As Integer, repaint As Boolean) As Boolean
    End Function
    Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure
End Class

Usage:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using New Centered_MessageBox(Me)
        MessageBox.Show("Test Text", "Test Title", MessageBoxButtons.OK)
    End Using
End Sub

Solution 2

Sadly there is no way to centre a MessageBox to a parent. It centres on the screen by default, and cannot be changed.

Solution 3

Create Your Own - Easy Create a form (get rid of controlbox in properties set false) Place Textbox (called TextBox_Prompt) and set it to multiline in properties Add 3 Buttons (wide/height enough to hold "CANCEL" comfortably) below the text box add below code to your form (I used the | character to denote a newline):

Public Class frmMsgBox
 Private mName As String = "Message Box"           ' default name for form
 Private mLocation As Point = New Point(400, 400)  ' default location in case user does set 
 Private mStyle As MsgBoxStyle
 Private mPrompt As String
 Private mResult As MsgBoxResult
 Private b1Result As MsgBoxResult
 Private b2Result As MsgBoxResult
 Private b3Result As MsgBoxResult
 Public WriteOnly Property Style As MsgBoxStyle
    Set(value As MsgBoxStyle)
        mStyle = value
    End Set
 End Property
 Public WriteOnly Property Prompt As String
    Set(value As String)
        mPrompt = value
    End Set
 End Property
 Public ReadOnly Property Result As MsgBoxResult
    Get
        Return mResult
    End Get
 End Property
 Public WriteOnly Property pLocation As Point
    Set(value As Point)
        mLocation = value
    End Set
 End Property
 Public WriteOnly Property sName As String
    Set(value As String)
        mName = value
    End Set
 End Property

 Private Sub frmMsgBox_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim strPrompt() As String = mPrompt.Split("|")   ' use | for splitting lines
    Dim sWidth As Integer = 0
    Dim sHeight As String = ""
    Me.Text = mName
    For Each sLine As String In strPrompt   ' get maximum width and height necessary for Prompt TextBox
        sWidth = Math.Max(sWidth, TextRenderer.MeasureText(sLine, TextBox_Prompt.Font).Width)
        sHeight += "@" + vbCrLf ' TextRenderer.MeasureText("@", TextBox_Prompt.Font).Height
        TextBox_Prompt.Text += sLine + vbCrLf
    Next
    TextBox_Prompt.Width = Math.Min(800, sWidth + 5)   ' set max width arbitrarily at 800
    TextBox_Prompt.Height = Math.Min(600, TextRenderer.MeasureText(sHeight, TextBox_Prompt.Font).Height)     ' set max height to 600 pixels
    Me.Width = Math.Max(Me.Width, TextBox_Prompt.Width + Me.Width - Me.ClientRectangle.Width + 20)
    TextBox_Prompt.Left = Math.Max(10, (Me.ClientRectangle.Width - TextBox_Prompt.Width) \ 2)
    Button1.Top = TextBox_Prompt.Top + TextBox_Prompt.Height + 20
    Button2.Top = Button1.Top : Button3.Top = Button1.Top
    Me.Height = Me.Height - Me.ClientRectangle.Height + 2 * TextBox_Prompt.Top + TextBox_Prompt.Height + Button1.Height + 20

    Dim Space2 As Integer = (Me.ClientRectangle.Width - 2 * Button1.Width) / 3
    Dim Space3 As Integer = (Me.ClientRectangle.Width - 3 * Button1.Width) / 4
    Select Case mStyle
        Case MsgBoxStyle.AbortRetryIgnore
            Button1.Text = "Abort" : Button2.Text = "Retry" : Button3.Text = "Ignore"
            Button1.Left = Space3
            Button2.Left = 2 * Space3 + Button1.Width
            Button3.Left = 3 * Space3 + 2 * Button1.Width
            b1Result = MsgBoxResult.Abort : b2Result = MsgBoxResult.Retry : b3Result = MsgBoxResult.Ignore
        Case MsgBoxStyle.YesNoCancel
            Button1.Text = "Yes" : Button2.Text = "No" : Button3.Text = "Cancel"
            Button1.Left = Space3
            Button2.Left = 2 * Space3 + Button1.Width
            Button3.Left = 3 * Space3 + 2 * Button1.Width
            b1Result = MsgBoxResult.Yes : b2Result = MsgBoxResult.No : b3Result = MsgBoxResult.Cancel
        Case MsgBoxStyle.YesNo
            Button1.Text = "Yes" : Button2.Text = "No" : Button3.Visible = False
            Button1.Left = Space2
            Button2.Left = 2 * Space2 + Button1.Width
            b1Result = MsgBoxResult.Yes : b2Result = MsgBoxResult.No
        Case MsgBoxStyle.OkCancel
            Button1.Text = "Ok" : Button2.Text = "Cancel" : Button3.Visible = False
            Button1.Left = Space2
            Button2.Left = 2 * Space2 + Button1.Width
            b1Result = MsgBoxResult.Ok : b2Result = MsgBoxResult.Cancel
        Case MsgBoxStyle.RetryCancel
            Button1.Text = "Retry" : Button2.Text = "Cancel" : Button3.Visible = False
            Button1.Left = Space2
            Button2.Left = 2 * Space2 + Button1.Width
            b1Result = MsgBoxResult.Retry : b2Result = MsgBoxResult.Cancel
        Case MsgBoxStyle.OkOnly
            Button1.Visible = False : Button2.Text = "Ok" : Button3.Visible = False
            Button1.Left -= Space2 : Button2.Width += 2 * Space2
            b2Result = MsgBoxResult.Ok
    End Select
    Me.Location = New Point(mLocation.X - Me.Width \ 2, mLocation.Y - Me.Height \ 2)
 End Sub
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    mResult = b1Result
    Me.Close()
 End Sub
 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    mResult = b2Result
    Me.Close()
 End Sub
 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    mResult = b3Result
    Me.Close()
 End Sub
End Class

to use your program you can do the following

Dim ans as MsgBoxResult
Using f As New frmMsgBox With {
            .sName = "form tile goes here ",
            .Style = MsgBoxStyle.YesNoCancel,            ' or whatever style
            .Prompt = "Your prompt|2nd line||4th line",
            .pLocation = New Point(Me.Left + Me.Width \ 2, Me.Top + Me.Height \ 2)
        }                            ' this location will center MsgBox on form
            f.ShowDialog()
            ans = f.Result
        End Using
        If ans = MsgBoxResult.Yes Then
           'do whatever
        ElseIf ans = MsgBoxResult.No then
            'do not whatever
        Else  ' was cancel
            ' do cancel
        End If

I use this form all the time You can also add a picture property/box to your form as well as other stuff. Georg

Solution 4

This is my own message box use C# winform, In addition to the realization of the parent form in the center, can customize the button text and icon. You can convert it to VB code yourself.

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace BluePointLilac.Methods
{
    /// <summary>在窗体居中显示的MessageBox</summary>
    public class MessageBoxEx
    {
        public static DialogResult Show(string text, string caption = null,
            MessageBoxButtons buttons = MessageBoxButtons.OK,
            MessageBoxIcon boxIcon = MessageBoxIcon.None)
        {
            using(MessageBoxForm frm = new MessageBoxForm(text, caption, buttons, boxIcon))
            {
                return frm.ShowDialog();
            }
        }

        public static string Show(string text, string caption,
            string[] buttonTexts, Image boxImaage, bool cancelBox = false)
        {
            using(MessageBoxForm frm = new MessageBoxForm(text, caption, buttonTexts, boxImaage, cancelBox))
            {
                frm.ShowDialog();
                return frm.Tag?.ToString();
            }
        }

        sealed class MessageBoxForm : Form
        {
            private MessageBoxForm(string text, string caption)
            {
                lblText.Text = text;
                this.Text = caption;
                this.Font = SystemFonts.MessageBoxFont;
                this.ShowIcon = this.ShowInTaskbar = false;
                this.MaximizeBox = this.MinimizeBox = false;
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
                this.StartPosition = FormStartPosition.CenterParent;
            }

            public MessageBoxForm(string text, string caption,
                string[] buttonTexts, Image boxImage, bool cancelBox) : this(text, caption)
            {
                this.CancelBox = cancelBox;
                this.InitializeComponents(buttonTexts, boxImage);
                foreach(Button button in flpButtons.Controls)
                {
                    button.Click += (sender, e) =>
                    {
                        this.Tag = button.Text;
                        this.Close();
                    };
                }
            }

            public MessageBoxForm(string text, string caption,
                MessageBoxButtons buttons, MessageBoxIcon boxIcon) : this(text, caption)
            {
                string[] buttonTexts = null;
                Image boxImage = null;
                switch(buttons)
                {
                    case MessageBoxButtons.OK:
                        buttonTexts = new[] { "OK" }; break;
                    case MessageBoxButtons.OKCancel:
                        buttonTexts = new[] { "Cancel", "OK" }; break;
                    case MessageBoxButtons.AbortRetryIgnore:
                        buttonTexts = new[] { "&Ignore", "&Retry", "&Abort" }; break;
                    case MessageBoxButtons.YesNoCancel:
                        buttonTexts = new[] { "Cancel", "&No", "&Yes" }; break;
                    case MessageBoxButtons.YesNo:
                        buttonTexts = new[] { "&No", "&Yes" }; break;
                    case MessageBoxButtons.RetryCancel:
                        buttonTexts = new[] { "Cancel", "&Retry" }; break;
                }
                switch(boxIcon)
                {
                    case MessageBoxIcon.Question:
                        boxImage = SystemIcons.Question.ToBitmap(); break;
                    case MessageBoxIcon.Error:
                        boxImage = SystemIcons.Error.ToBitmap(); break;
                    case MessageBoxIcon.Warning:
                        boxImage = SystemIcons.Warning.ToBitmap(); break;
                    case MessageBoxIcon.Information:
                        boxImage = SystemIcons.Information.ToBitmap(); break;
                }
                this.CancelBox = !buttonTexts.Contains("Cancel");
                this.InitializeComponents(buttonTexts, boxImage);
                foreach(Button button in flpButtons.Controls)
                {
                    button.Click += (sender, e) =>
                    {
                        switch(button.Text)
                        {
                            case "OK":
                                this.DialogResult = DialogResult.OK; break;
                            case "Cancel":
                                this.DialogResult = DialogResult.Cancel; break;
                            case "&Yes":
                                this.DialogResult = DialogResult.Yes; break;
                            case "&No":
                                this.DialogResult = DialogResult.No; break;
                            case "&Abort":
                                this.DialogResult = DialogResult.Abort; break;
                            case "&Retry":
                                this.DialogResult = DialogResult.Retry; break;
                            case "&Ignore":
                                this.DialogResult = DialogResult.Ignore; break;
                        }
                    };
                }
            }

            private void InitializeComponents(string[] buttonTexts, Image boxImage)
            {
                int w1 = 0;
                Size buttonSize = new Size(75, 27);
                for(int i = 0; i < buttonTexts.Length; i++)
                {
                    Button button = new Button
                    {
                        Margin = new Padding(0, 12, 12, 12),
                        Parent = flpButtons,
                        AutoSize = true,
                        Text = buttonTexts[i],
                    };
                    button.Width = Math.Max(buttonSize.Width, button.Width);
                    button.Height = Math.Max(buttonSize.Height, button.Height);
                    buttonSize = button.Size;
                    w1 += button.Width + button.Margin.Horizontal;
                }
                picIcon.Image = boxImage;
                if(boxImage == null)
                {
                    picIcon.Visible = false;
                    lblText.Left = 35;
                }
                pnlInfo.Controls.AddRange(new Control[] { picIcon, lblText });
                this.Controls.AddRange(new Control[] { pnlInfo, flpButtons });
                pnlInfo.Height = lblText.Height + lblText.Top * 2;
                int w2 = lblText.Right + picIcon.Left;
                int w = Math.Max(w1, w2);
                int h = pnlInfo.Height + flpButtons.Height;
                this.ClientSize = new Size(w, h);
            }

            readonly FlowLayoutPanel flpButtons = new FlowLayoutPanel
            {
                Padding = new Padding(0, 0, 12, 0),
                FlowDirection = FlowDirection.RightToLeft,
                BackColor = default,
                Dock = DockStyle.Bottom,
                Height = 50
            };
            readonly Panel pnlInfo = new Panel
            {
                BackColor = Color.White,
                Dock = DockStyle.Top,
            };
            readonly PictureBox picIcon = new PictureBox
            {
                SizeMode = PictureBoxSizeMode.AutoSize,
                Location = new Point(30, 30)
            };
            readonly Label lblText = new Label
            {
                Location = new Point(67, 32),
                AutoSize = true,
            };

            readonly bool CancelBox = false;

            protected override CreateParams CreateParams
            {
                get
                {
                    const int CP_NOCLOSE_BUTTON = 0x200;
                    CreateParams cp = base.CreateParams;
                    if(CancelBox) cp.ClassStyle |= CP_NOCLOSE_BUTTON; //禁用关闭按钮
                    return cp;
                }
            }

            protected override bool ProcessDialogKey(Keys keyData)
            {
                if(ModifierKeys == Keys.None)
                {
                    if(!CancelBox && keyData == Keys.Escape)
                    {
                        this.DialogResult = DialogResult.Cancel; //ESC键关闭窗体并返回“取消”
                        return true;
                    }
                }
                return base.ProcessDialogKey(keyData);
            }

            protected override void OnLoad(EventArgs e)
            {
                this.Owner = Form.ActiveForm;
                if(this.Owner == null) this.StartPosition = FormStartPosition.CenterScreen;
                base.OnLoad(e);
            }
        }
    }
}
Share:
22,077
ElektroStudios
Author by

ElektroStudios

Updated on July 26, 2021

Comments

  • ElektroStudios
    ElektroStudios almost 3 years

    There is a way to center a MessageBox without subclassing or hooking?

    I'm looking for VB.NET code.