Download Sample .NET project for MessageBox program
This chapter and the attached sample application will give you a brief idea about the following:
Reading the text value from a textbox
Displaying a message box
About the sample application This application has 1 textbox and a button. If you type anything in the textbox and press the button, it will display the same text you typed in a MessageBox.
Download and unzip the sample application from this page. After you unzip, you will get several files. Some of the important files are :
1. Chapter1.csproj - This is the C# project file. Double click this file to
open the project.
2. Form1.cs - This is the file for the Form1 in teh sample application.
After you open the project, see the 'Solution Explorer' bar on the right side of the Visual Studio. You can click on that to expand it, which will display the list of all files in the project. (Or, you can go to the menu 'View > Solution Explorer' to open the solution explorer.
Click on 'Form1.cs' in the solution explorer to open the Form. Now you can see the Form in 'design mode'. Right click on the 'Form1.cs' in the solution explorer and choose 'View Code' to see the source code behind this Form (Or, press F7 from the Form to see the code).
Let us analyze the code:
The code has lot of Visual Studio generated code. For timebeing ignore all those code and scroll down to the event handler code in the bottom of the file.
System.Windows.Forms.MessageBox.Show( "You typed : " + txtMessage.Text );
DialogResult result = MessageBox.Show( "Did you like this application ?",
"Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if ( result == DialogResult.Yes )
MessageBox.Show("You selected YES");
MessageBox.Show("You selected NO");
The first line will simply display a messagebox and show the text typed by the user. 'MessageBox' is a class inside the namespace 'System.Windows.Forms'. We have a statement 'using System.Windows.Forms' on top of the source code. This is like a source code. This will help us avoid typing 'System.Windows.Forms.' everytime when we want to use a class inside this namespace.
The next few lines of code do a bit more. Here we are displaying a message box, with some additional attributes.
The second optional parameter of the 'Show' method is the 'caption' for the messagebox.
The third parameter tells that the messagebox will have two buttons - YES and NO. You have several other options like MessageBoxButtons.OK, MessageBoxButtons.OKCancel etc.
And, the last parameter says 'display a question Icon in the messagebox'. There are many other types of icons you can display.
And another important part is, the MessageBox returns a value when the user press a button to close the messagebox. Our MessageBox asks a question 'Did you like this application ?" and gives two options to the user - YES or NO.
The return value is of type 'DialogResult' and is assigned to the variable 'result'. We are checking the value of the result and depending on whether user pressed the YES button or NO button, appropriate message will be displayed.