C# MessageBox.Show

http://www.dotnetperls.com/messagebox-show

Dialog boxes interrupt users. They force users to respond before further action is taken. This is necessary in some situations. It is useful if a warning is important or an serious error occurred. TheMessageBox.Show method is the easiest way to display a dialog box. It is available in C# Windows Forms programs.

Examples of MessageBox.Show in Windows Forms

Examples

To start, the MessageBox.Show method is a static method, which means you do not need to create a new MessageBox() anywhere in your code. Instead, you can simply type "MessageBox" and press the period, and then select Show.

The following example shows the MessageBox.Show method used in the Form1_Load event handler. To make the Form1_Load event handler, create a new Windows Forms application and double-click on the window in the designer.

Windows Forms program that uses MessageBox [C#]

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;

namespace WindowsFormsApplication16
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
	    //
	    // The simplest overload of MessageBox.Show. [1]
	    //
	    MessageBox.Show("Dot Net Perls is awesome.");
	    //
	    // Dialog box with text and a title. [2]
	    //
	    MessageBox.Show("Dot Net Perls is awesome.",
		"Important Message");
	    //
	    // Dialog box with two buttons: yes and no. [3]
	    //
	    DialogResult result1 = MessageBox.Show("Is Dot Net Perls awesome?",
		"Important Question",
		MessageBoxButtons.YesNo);
	    //
	    // Dialog box with question icon. [4]
	    //
	    DialogResult result2 = MessageBox.Show("Is Dot Net Perls awesome?",
		"Important Query",
		MessageBoxButtons.YesNoCancel,
		MessageBoxIcon.Question);
	    //
	    // Dialog box with question icon and default button. [5]
	    //
	    DialogResult result3 = MessageBox.Show("Is Visual Basic awesome?",
		"The Question",
		MessageBoxButtons.YesNoCancel,
		MessageBoxIcon.Question,
		MessageBoxDefaultButton.Button2);
	    //
	    // Test the results of the previous three dialogs. [6]
	    //
	    if (result1 == DialogResult.Yes &&
		result2 == DialogResult.Yes &&
		result3 == DialogResult.No)
	    {
		MessageBox.Show("You answered yes, yes and no.");
	    }
	    //
	    // Dialog box that is right-aligned (not useful). [7]
	    //
	    MessageBox.Show("Dot Net Perls is the best.",
		"Critical Warning",
		MessageBoxButtons.OKCancel,
		MessageBoxIcon.Warning,
		MessageBoxDefaultButton.Button1,
		MessageBoxOptions.RightAlign,
		true);
	    //
	    // Dialog box with exclamation icon. [8]
	    //
	    MessageBox.Show("Dot Net Perls is super.",
		"Important Note",
		MessageBoxButtons.OK,
		MessageBoxIcon.Exclamation,
		MessageBoxDefaultButton.Button1);
	}
    }
}
Note

In the F


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值