Windows message box buttons

Message Box Class

Definition

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.

Examples

The following code example shows how to use a MessageBox to inform the user of a missing entry in a TextBox. This example requires that the method is called from an existing form with a TextBox named ServerName on it.

The following code example shows how to ask the user a yes or no question and make a decision based on the response.

Remarks

You cannot create a new instance of the MessageBox class. To display a message box, call the static method MessageBox.Show. The title, message, buttons, and icons displayed in the message box are determined by parameters that you pass to this method.

Methods

Determines whether the specified object is equal to the current object.

(Inherited from Object) GetHashCode()

Serves as the default hash function.

(Inherited from Object) GetType()

Gets the Type of the current instance.

(Inherited from Object) MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object) Show(IWin32Window, String)

Displays a message box in front of the specified object and with the specified text.

Displays a message box in front of the specified object and with the specified text and caption.

Displays a message box in front of the specified object and with the specified text, caption, and buttons.

Displays a message box in front of the specified object and with the specified text, caption, buttons, and icon.

Displays a message box in front of the specified object and with the specified text, caption, buttons, icon, and default button.

Displays a message box in front of the specified object and with the specified text, caption, buttons, icon, default button, and options.

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file.

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file and HelpNavigator .

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file, HelpNavigator , and Help topic.

Читайте также:  Код ошибки windows 103

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file and Help keyword.

Displays a message box with specified text.

Displays a message box with specified text and caption.

Displays a message box with specified text, caption, and buttons.

Displays a message box with specified text, caption, buttons, and icon.

Displays a message box with the specified text, caption, buttons, icon, and default button.

Displays a message box with the specified text, caption, buttons, icon, default button, and options.

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button.

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file.

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file and HelpNavigator .

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file, HelpNavigator , and Help topic.

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file and Help keyword.

Returns a string that represents the current object.

Windows message box buttons

Answered by:

Question

Do you know a solution to add custom MessageBoxButtons?

I need a Print button and by default there are only: yes, no, cancel, ok, abort, retry and ignore buttons.

I know that I can create a new form to act as a MessageBox, but I must add manually MessageBoxIcons on my forms and make a new form for every language that I use.

Answers

Based on my understanding, inherite the MessageBoxButton wont make sense.You have to make a new form to realize that.

Make a Form like below:

public class MyButtonsMessageBox : Form
<

private void InitializeComponent()
<
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
//here is code created by visual studio

button1.DialogResult = System.Windows.Forms.DialogResult.OK;
//
// button2
//
//here is code created by visual studio

button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
//
// Form2
//

// Define the border style of the form to a dialog box
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
// Set the accept button of the form to button1.
this.AcceptButton = button1;
// Set the cancel button of the form to button2.
this.CancelButton = button2;

this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = «Form2»;
this.Text = «Form2»;
this.ResumeLayout(false);

MyButtonsMessageBox form1 = new MyButtonsMessageBox ();

Читайте также:  Windows 10 просмотр dlna

// Display the form as a modal dialog box.
form1.ShowDialog();

// Determine if the OK button was clicked on the dialog box.
if (form1.DialogResult == DialogResult.OK)
<
//Do you work here
>
else if(form1.DialogResult == DialogResult.Cancel)
<

Hope this can help you.

All replies

Yeah you have to make your own form. Just remember to set the dialog results when one of the buttons are clicked and then close the form. When you want to use the form use the ShowDialog method which returns the dialog return. You can even make your own static methods so your class acts just like the messagebox class. A small example is below.

public class MyMessageBox : Form
<
public static DialogResult ShowMessage(string message, string caption)
<
using (MyMessageBox mybox = new MyMessageBox())
<
mybox.Text = caption;
mybox.LabelMessage.Text = message;
return mybox.ShowDialog();
>

It is possible to extend button enumeration from System.Windows.Forms. MessageBoxButtons ?

It is possible to extend button enumeration from System.Windows.Forms. MessageBoxButtons ?

«Extend» Nope. No inheritance if that is what you mean.

You could define your own enum with the same name. Your code could even be configured to use it instead of the one enum defined in the Framework. But, which enum will methods in the Framework use?

I need only a different text on the button, but I will create a new messagebox with my own list of buttons.

Based on my understanding, inherite the MessageBoxButton wont make sense.You have to make a new form to realize that.

Make a Form like below:

public class MyButtonsMessageBox : Form
<

private void InitializeComponent()
<
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
//here is code created by visual studio

button1.DialogResult = System.Windows.Forms.DialogResult.OK;
//
// button2
//
//here is code created by visual studio

button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
//
// Form2
//

// Define the border style of the form to a dialog box
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
// Set the accept button of the form to button1.
this.AcceptButton = button1;
// Set the cancel button of the form to button2.
this.CancelButton = button2;

this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = «Form2»;
this.Text = «Form2»;
this.ResumeLayout(false);

MyButtonsMessageBox form1 = new MyButtonsMessageBox ();

// Display the form as a modal dialog box.
form1.ShowDialog();

// Determine if the OK button was clicked on the dialog box.
if (form1.DialogResult == DialogResult.OK)
<
//Do you work here
>
else if(form1.DialogResult == DialogResult.Cancel)
<

Hope this can help you.

This is a custom message box form that is called in the same way as MessageBox.Show

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

Читайте также:  Drivers notebook asus windows 10

namespace CustomMessageBox
<
public partial class FMessageBox : Form
<
public FMessageBox()
<
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
button2.Click += new EventHandler(button2_Click);
button3.Click += new EventHandler(button3_Click);
>

void button3_Click(object sender, EventArgs e)
<
ButtonClicked = EButtonClicked.button3;
Close();
>

void button2_Click(object sender, EventArgs e)
<
ButtonClicked = EButtonClicked.button2;
Close();
>

void button1_Click(object sender, EventArgs e)
<
ButtonClicked = EButtonClicked.button1;
Close();
>

public enum EButtonClicked
<
button1,
button2,
button3,
notSet
>

public EButtonClicked ButtonClicked = EButtonClicked.notSet;

public static EMessageReturn Show(string aCaption, string aMessage, EMessageButtons eMessageButtons)
<
FMessageBox fMessageBox = new FMessageBox();
fMessageBox.Text = aCaption;
fMessageBox.tbMessage.Text = aMessage;
if (eMessageButtons == EMessageButtons.RetryExit)
<
fMessageBox.button1.Text = «Exit»;
fMessageBox.button2.Text = «Retry»;
fMessageBox.button3.Visible = false;

switch (fMessageBox.ButtonClicked)
<
case EButtonClicked.button1:
return EMessageReturn.Exit;
case EButtonClicked.button2:
return EMessageReturn.Retry;
>
>
return EMessageReturn.NotSet;
>
>

public enum EMessageButtons
<
RetryExit,
>

public enum EMessageReturn
<
Retry,
Exit,
NotSet
>
>

namespace CustomMessageBox
<
partial class FMessageBox
<
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///
///

true if managed resources should be disposed; otherwise, false.

#region Windows Form Designer generated code

Message Box Default Button Перечисление

Определение

Задает константы, определяющие кнопку по умолчанию в MessageBox. Specifies constants defining the default button on a MessageBox.

Первая кнопка в окне сообщения является кнопкой по умолчанию. The first button on the message box is the default button.

Вторая кнопка в окне сообщения является кнопкой по умолчанию. The second button on the message box is the default button.

Третья кнопка в окне сообщения является кнопкой по умолчанию. The third button on the message box is the default button.

Примеры

В следующем примере кода показано, как отобразить MessageBox с параметрами, поддерживаемыми этой перегрузкой Show . The following code example demonstrates how to display a MessageBox with the options supported by this overload of Show. После проверки того, что строковая переменная ServerName пуста, в примере отображается MessageBox значок с полем вопроса, предлагающий пользователю отменить операцию. After verifying that a string variable, ServerName , is empty, the example displays a MessageBox with a question box icon, offering the user the option to cancel the operation. В примере используется RightAlign элемент MessageBoxOptions перечисления для выровняйте текст по правому краю диалогового окна. The example uses the RightAlign member of the MessageBoxOptions enumeration to align the text to the right edge of the dialog box. Если Show возвращаемое значение метода равно Yes , то форма, на которой отображается, MessageBox закрывается. If the Show method’s return value evaluates to Yes, the form that displayed the MessageBox is closed.

Комментарии

Это перечисление используется классом MessageBox. This enumeration is used by the MessageBox class.

Оцените статью