How do I display a message box in my form application?

To show the message box, call the MessageBox class’ Show() method. The message box is the modal form that notifies the user about certain event in the application and optionally takes the user feedback. The Show() is an static (C#) / Shared (VB.NET) method of the System.Windows.Forms.MessageBox class. It has a number of overloaded versions. The simplest one take the message text and display it in the message box.

C# Version

MessageBox.Show("Welcome to Win Form");


VB.NET Version

MessageBox.Show("Welcome to Win Form")


To add the title to the message box, call the MessageBox.Show() like:

C# Version

MessageBox.Show("Welcome to Win Form", "Greeting");

VB.NET Version

MessageBox.Show("Welcome to Win Form", "Greeting")


The following example code shows the message box with Yes, No, Cancel button to confirm application close and closes the application if the user selects the Yes button

C# Version

DialogResult dlgRes = null
dlgRes = MessageBox.Show(
	"Are you sure you want to close without saving the document", 
	"Confirm Document Close", 
MessageBoxButtons.YesNoCancel, 
	MessageBoxIcon.Question);
if(dlgRes == DialogResult.Yes)
{
	Application.Exit();
}


VB.NET Version

Dim dlgRes As DialogResult
dlgRes = MessageBox.Show( _
      "Are you sure you want to close without saving the document", _
      "Confirm Document Close", _
MessageBoxButtons.YesNoCancel, _
      MessageBoxIcon.Question)
If dlgRes = DialogResult.Yes Then
      Application.Exit()
End If


Index

 
Printer friendly version of the FAQ-WinForm-MessageBox page


Sponsored links

.Net Application Updating
One easy to use component adds safe and reliable updating features. Download today for a free trial.
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Villanova University Six Sigma & IT Certificate Programs
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
.Net Localization in three simple steps (WYSIWYG)
Localize .Net, C#/C/C++ & Delphi apps visually. HTML, HTML Help, XML & databases. Try Sisulizer now!

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.