MessageBox
In the System.Windows.Forms namespace you have the MessageBox class.
link - learn.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox?view=net-5.0
System.Windows.Forms.MessageBox.Show(
System.Windows.Forms.IWin32Window owner,
string text,
string caption, _
System.Windows.Forms.MessageBoxButtons buttons
System.Windows.Forms.MessageBoxIcon icon
System.Windows.Forms.MessageBoxDefaultButton defaultbutton
System.Windows.Forms.MessageBoxOptions options)
As System.Windows.Forms.DialogResult
MessageBoxButtons
| AbortRetryIgnore | |
| OK | |
| OKCancel | |
| RetryCancel | |
| YesNo | |
| YesNoCancel | |
MessageBoxIcon
| None | The message box contain no symbols. |
| Information | The message box contains a symbol consisting of a lowercase letter i in a circle. |
| Asterisk | same as above |
| Error | The message box contains a symbol consisting of white X in a circle with a red background. |
| Hand | same as above |
| Stop | same as above |
| Exclamation | The message box contains a symbol consisting of an exclamation point in a triangle with a yellow background. |
| Warning | same as above |
| Question | The message box contains a symbol consisting of a question mark in a circle. |
MessageBoxDefaultButton
| Button1 | The first button on the message box is the default button. |
| Button2 | The second button on the message box is the default button. |
| Button3 | The third button on the message box is the default button. |
MessageBoxOptions
| DefaultDesktopOnly | The message box is displayed on the active desktop. This constant is the same as ServiceNotification except that the system displays the message box only on the default desktop of the interactive window station DefaultDesktopOnly will cause the application that raised the MessageBox to lose focus. The MessageBox that is displayed will not use visual styles. For more information, see Rendering Controls with Visual Styles. |
| RightAlign | The message box text is right-aligned. |
| RtlReading | Specifies that the message box text is displayed with right to left reading order. |
| ServiceNotification | The message box is displayed on the active desktop. The caller is a service notifying the user of an event. The function displays a message box on the current active desktop, even if there is no user logged on to the computer. |
DialogResult
| Abort | |
| Cancel | |
| Ignore | |
| No | |
| None | |
| OK | |
| Retry | |
| Yes | |
Do Not Show Again
It is possible to include a checkbox on your messages that says "don't show this message again"
This can be achieved by using a windows API call to the following function
shlwapi.dll - SHMessageBoxCheck
MessageBox Examples
Example 1 - Information
System.Windows.Forms.MessageBox.Show(
"This is a message",
"Message Box Caption",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
Example 2 - Multiple Lines
System.Windows.Forms.MessageBox.Show(
"This is a message" +
System.Environment.NewLine +
System.Environment.NewLine +
"Displayed over multiple lines",
"Message Box Caption",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
Example 3 - Exclamation / Warning
System.Windows.Forms.MessageBox.Show(
"This is a message",
"Message Box Caption",
System.Windows.Forms.MessageBoxButtons.OKCancel,
System.Windows.Forms.MessageBoxIcon.Exclamation);
Example 4 - Error / Stop / Hand
System.Windows.Forms.MessageBox.Show(
"This is a message",
"Message Box Caption",
System.Windows.Forms.MessageBoxButtons.OKCancel,
System.Windows.Forms.MessageBoxIcon.Error);
Example 5 - Question
System.Windows.Forms.DialogResult oResult;
oResult = System.Windows.Forms.MessageBox.Show(
"This is a question ?",
"Message Box Caption",
System.Windows.Forms.MessageBoxButtons.YesNoCancel,
System.Windows.Forms.MessageBoxIcon.Question);
if (oResult == System.Windows.Forms.DialogResult.Yes)
{ }
if (oResult == System.Windows.Forms.DialogResult.No)
{ }
if (oResult == System.Windows.Forms.DialogResult.Cancel)
{ }
Default Buttons
MessageBoxDefaultButton.Button1,
Message Options
MessageBoxOptions.RightAlign,
Center of Parent
A message box is always centered on the screen.
It is possible to display a message box in the middle of the Application window.
This approach retrieves the position and size of the application window and then moves the message box window.
Three Windows API functions are used in this solutions.
public struct structure_RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public extern bool GetWindowRect(System.IntPtr hWnd, ref structure_RECT rect)
public extern IntPtr FindWindow(string lpClassName, string lpWindowName)
public extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
MessageBox_CenterApplication(caption);
System.Windows.Forms.MessageBox.Show(smessage,
caption,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
Constants.gTHREAD_MESSAGEBOXCENTER.Abort();
Constants.FOUND_ERROR = true;
public static void MessageBox_CenterApplication(
string caption)
{
try
{
Constants.Structure_RECT oRect;
System.IntPtr ohandle;
System.Windows.Forms.NativeWindow nativeWindow;
oRect = new Constants.Structure_RECT();
Constants.gMESSAGEBOX_APPLICATION_HANDLE = new System.IntPtr(Constants.gEXCELAPPLICATION.Hwnd);
ohandle = Constants.gMESSAGEBOX_APPLICATION_HANDLE;
//ohandle = Code.Method_HandleGet(Globals.ThisAddIn.Application);
nativeWindow = new System.Windows.Forms.NativeWindow();
nativeWindow.AssignHandle(ohandle);
Constants.GetWindowRect(ohandle, ref oRect);
nativeWindow.ReleaseHandle();
int ExcelWindow_CenterX = oRect.Left + (oRect.Right - oRect.Left) / 2;
int ExcelWindow_CenterY = oRect.Top + (oRect.Bottom - oRect.Top) / 2;
Constants.gTHREAD_MESSAGEBOXCENTER = new System.Threading.Thread(() =>
{
Constants.Structure_RECT oRect2;
System.IntPtr ohandle2;
oRect2 = new Constants.Structure_RECT();
while ((ohandle2 = Constants.FindWindow(System.IntPtr.Zero, caption)) == System.IntPtr.Zero)
{
}
Constants.GetWindowRect(ohandle2, ref oRect2);
int Msgbox_Left = ExcelWindow_CenterX - (int)(oRect2.Right - oRect2.Left) / 2;
int Msgbox_Top = ExcelWindow_CenterY - (int)(oRect2.Bottom - oRect2.Top) / 2;
Msgbox_Left = (Msgbox_Left < 0) ? 10 : Msgbox_Left;
Msgbox_Top = (Msgbox_Top < 0) ? 10 : Msgbox_Top;
int result = Constants.MoveWindow(ohandle2, Msgbox_Left, Msgbox_Top,
oRect2.Right - oRect2.Left,
oRect2.Bottom - oRect2.Top, true);
});
Constants.gTHREAD_MESSAGEBOXCENTER.Start();
//Must include this after the MessageBox.Show
//Constants.gTHREAD_MESSAGEBOXCENTER.Abort();
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
//ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
Owner
If you are displaying a message box inside Excel, then Excel is the owner by default.
For this reason you do not need to use the owner parameter inside a VSTO add-in.
Assigning the owner parameter has nothing to do with displaying in the Centre of Parent.
public static Microsoft.Office.Interop.Excel.Application gEXCELAPPLICATION;
public class Win32Window : System.Windows.Forms.IWin32Window
{
private IntPtr _handle;
public Win32Window(System.IntPtr oHandle)
{
this._handle = oHandle;
}
public Win32Window(int oHandle)
{
this._handle = new System.IntPtr(oHandle);
}
System.IntPtr System.Windows.Forms.IWin32Window.Handle
{
get { return this._handle; }
}
}
System.Windows.Forms.IWin32Window owner;
owner = new Win32Window(Constants.gEXCELAPPLICATION.Hwnd);
System.Windows.Forms.MessageBox.Show(owner,
sText, sCaption,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation);
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext