Chapter 1 . Getting Started With C# .
NET
C# Tutorial by M. Abdollahi
(Isfahan University of Technology – IT Center)
Barbod Engineering Co.
Oct. 2011
http://www.abdollahi.us
Agenda
What we're going to do first is to create a very
simple program, so that you can see what makes
up a C# .NET project. By the end of this chapter,
you'll have learnt the following:
How to create new projects
What the Solution Explorer is
The various files that make up of a C# .NET project
How to save your work
How to run programs
The importance of the Main statement
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 2/80
Console Application
A Console Application is one that looks like a DOS
window. If you don't know what these are, click
your Start menu in the bottom left of your screen.
Click on Run. From the dialogue box that appears,
type cmd:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 3/80
Console Application
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 4/80
Console Application
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 5/80
Console Application
This is the type of window you'll see for our
Console Application. When you create your
Windows forms, there's a whole lot of code to
get used to. But Console Applications start off
fairly simple, and you can see which part of
the program is the most important.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 6/80
Console Application
Start
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 7/80
Console Application
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 8/80
Console Application
Solution
Explorer
Code Window
this code is the program that
will run when anyone starts
your application.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 9/80
Console Application
Now click the plus symbol next to Properties
in the Solution Explorer above. You'll see the
following:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 10/80
Console Application
The file called AssemblyInfo.cs contains
information about your program. Double
click this file to open it up and see the code.
Here's just some of it: The reddish colour text is something
you can change. You can add a Title,
Description, Copyright, Trademark, etc.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 11/80
Console Application
Now, in the Solution Explorer, click the plus
symbol next to References:
These are references to code
built in to C# (you won't see
as many entries in earlier
versions of the software).
Much later, you'll see how to
add your own files to this
section.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 12/80
Saving Your Code
When you save your work, C# will create
quite a few folders and files for you. Click File
from the menu bar at the top of the Visual
Studio software, then Save All.
When you browse the project directory you'll
see the following
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 13/80
Saving Your Code
So there's a folder called ConsoleApplication1. There's
also two files: one that ends in sln, and one that ends
in suo. The sln file is the entire solution. Have a look at
the Solution Explorer again:
The one highlighted in
blue at the top refers to
the sln file.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 14/80
Saving Your Code
The suo file contains information about the Visual
Studio environment - whether the plus symbols
are expanded in the Solution Explorer, what other
files you have open in this project, and a whole
host of other settings. (If you can't see the suo file
click Tools > Folder Option in Windows Explorer.
In Vista and Windows 7, you may have to
click Organise > Layout > Menu Bar first. Click
the View tab, and select the option for "Show
hidden files and folders".)
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 15/80
Saving Your Code
Double click your ConsoleApplication1 folder,
though, to see inside of it:
Now we have three more folders and
two files. You can see the bin and obj
folders in the Solution Explorer: 1
2
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 16/80
A Simple C# Console Application
The only thing we'll do with the code is to write
some text to the screen. But here's the code that
Visual Studio prepares for you when you first create
a Console Application:
3 lines that start with using
add references to built-in
code. The namespace line
includes the name of your
application. A namespace is a
way to group related code
together. Again, don't worry
about the term namespace, as
you'll learn about these later.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 17/80
A Simple C# Console Application
The thing that's important above is the word class.
All your code will be written in classes. This one is
called Program (you can call them anything you
like, as long as C# hasn't taken the word for itself).
But think of a class as a segment of code that you
give a name to.
Inside of the class called Program there is this code:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 18/80
A Simple C# Console Application
This piece of code is something called
a Method. The name of the Method above is
Main. When you run your program, C# looks
for a Method called Main. It uses the Main
Method as the starting point for your
programs. It then executes any code between
those two curly brackets. The blue words
above are all special words - keywords. You'll
learn more about them in later chapters.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 19/80
A Simple C# Console Application
position your cursor after the first curly
bracket, and then hit the enter key on your
keyboard:
The cursor automatically indents for you, ready
to type something. Note where the curly
brackets are, though, in the code above. You
have a pair for class Program, and a pair for the
Main method. Miss one out and you'll get error
messages.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 20/80
A Simple C# Console Application
The single line of code we'll write is this (but
don't write it yet):
Console.WriteLine("Hello C Sharp!");
First, type the letter "C". You'll see a popup
menu. This popup menu is called the IntelliSense
menu. It tries to guess what you want, and
allows you to quickly add the item from the list.
But it should look like this, after you have typed
a capital letter "C“ :
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 21/80
A Simple C# Console Application
C# 2010 OLDER VERSIONS OF C#
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 22
A Simple C# Console Application
The icon to the left of the word Console on
the list above means that it is a Class. But
press the Enter key on your keyboard. The
word will be added to your code:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 23/80
A Simple C# Console Application
Now type a full stop (period) immediately after the
word Console. The IntelliSense menu appears again:
You can use the arrow keys on your keyboard
to move up or down the list. But if you type
Write and then the letter L of Line,
IntelliSense will automatically move down and
select it for you:
C# 2010
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 24/80
A Simple C# Console Application
Press the Enter key to add the word WriteLine to
your code:
Now type a left round bracket. As soon as you type
the round bracket, you'll see this:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 25/80
A Simple C# Console Application
WriteLine is another Method (A Method is just some
code that does a particular job). But the yellow box
is telling you that there are 19 different versions of
this Method. You could click the small arrows to
move up and down the list. Instead, type the
following:
"Hello C Sharp!"
Don't forget the double quotes at
the start and end. These tell C#
that you want text. Your code will
look like this:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 26/80
A Simple C# Console Application
Now type a right round bracket:
Notice the red wiggly line at the end.
This is the coding environment's way
of telling you that you've missed
something out.
The thing we've missed out is a semicolon. All complete lines
of code in C# must end with a semicolon. Miss one out and
you'll get error messages. Type the semicolon at the end and
the red wiggly line will go away. Your code should now look
like this:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 27/80
A Simple C# Console Application
Note all the different colors. Visual Studio color-
codes the different parts of your code. The
reddish color between double quotes means
that you want text; the green color means it's a
Class; blue words are ones that C# reserves for
itself.
(If you want, you can change these colors. From
the menu bar at the top, click Tools > Options.
Under Environment, click Fonts and Colors.)
Time now to Build and Run your code!
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 28/80
How to Run your C# Program
You can test your program a number of ways. First,
though, it has to be built. This is when everything is
checked to see if there are any errors. Try this:
From the View menu at the top of Visual Studio or Visual
Studio, click Output. You’ll see a window appear at the
bottom. (In C# 2010, if you can't see an Output entry, click
the Tools menu. From the Tools menu, select Settings >
Expert Settings. The Output menu item should then
appear on the View menu.)
From the Build menu at the top of Visual Studio ,
click Build Solution
You should see the following report:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 29/80
How to Run your C# Program
The final line is this:
Build: 1 succeeded or up-to-date, 0 failed, 0 skipped
That's telling you that everything is OK.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 30/80
How to Run your C# Program
Now try this:
Delete the semicolon from the end of your line of
code
Click Build > Build Solution again
Examine the output window
This time, you should see these two lines at the end of
the report:
Compile complete -- 1 errors, 0 warnings
Build: 0 succeeded or up-to-date, 1 failed, 0
skipped
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 31/80
How to Run your C# Program
So it's telling you that it couldn't build your
solution because there was 1 error.
Put the semicolon back at the end of the line.
Now click Debug from the menu at the top of
Visual Studio. From the Debug menu,
select Start Debugging.
You should see a black DOS window appear
and then disappear. Your program has run
successfully!
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 32/80
How to Run your C# Program
To actually see your line of text, click Debug >
Start Without Debugging. You should now
see this:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 33/80
How to Run your C# Program
And that's your program! Have a look at the
Solution Explorer on the right. Because the project
has been built, you'll see two more files under
Debug:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 34/80
How to Run your C# Program
However, in C# 2010 you'll see a Release
folder . Expand this:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 35/80
How to Run your C# Program
We now have a ConsoleApplication1.exe and
ConsoleApplication1.pdb. The exe file is an
executable program, and it appears in the
bin/debug folder. Switch back to Windows Explorer,
if you still have it open. You'll see the exe file there.
(In the Release folder in C# 2010, but in the Debug
folder in earlier versions):
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 36/80
How to Run your C# Program
You could, if you wanted, create a desktop
shortcut to this exe file. When you double
click the desktop shortcut, the program will
run.
But that's enough of Console Applications -
we'll move on to creating Windows
Applications.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 37/80
Your First C# Windows Form
From now on, we're going to be creating
Windows Applications, rather than Console
Applications. Windows Applications make use
of something called a Form. The Form is
blank at first. You then add control to your
form, things like buttons, text boxes, menus,
check boxes, radio buttons, etc. To get your
first look at a Windows Form, do the
following.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 38/80
Your First C# Windows Form
If you still have your Console Application open from
the previous section, click File from the menu bar at
the top of Visual Studio . From the File menu, click
on Close Solution.
To create your first Windows form project, click the
File menu again. This time, select New Project from
the menu. When you do, you'll see the New Project
dialogue box again:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 39/80
Your First C# Windows Form
Select Windows
Forms Application
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 40/80
Your First C# Windows Form
When you click OK, a new Windows Application
project will be created for you:
If Toolbox tab look likes this put mouse cursor
on it to open it
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 41/80
Your First C# Windows Form
If you want to fix Toolbox you should click on
Auto Hide mark and change it’s status from
horizontal to vertical
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 42/80
Your First C# Windows Form
The obvious difference from the Console
Application you created in the previous
section is the blank Form in the main window.
Notice the Toolbox, though, on the left hand
side. We'll be adding controls from the
Toolbox to that blank Form1 .
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 43/80
Your First C# Windows Form
Notice the Solution Explorer on the right side
of your screen . If you compare it with the
Solution Explorer when you created your
Console Application, you'll see there's only
one difference - the Form.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 44/80
Your First C# Windows Form
We still have the Properties, the References and the
Program.cs file. Double click the Program.cs file to open
it, and you'll see some familiar code:
Both have the same using lines , a
namespace , a class called Program,
and a Main Method.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 45/80
Your First C# Windows Form
The Main Method is the entry point for your program.
The code between the curly brackets of Main will get
executed when the program first starts. The last line in
the WindowsApplication1 code above is the one that
Runs Form1 when the Application starts.
You can do other things here. For example, suppose
you had a program that connects to a server. If it finds
a connection then it loads some information from a
database. In the Main Method, you could check that
the server connection is OK. If it's not, display a
second form; if it's OK, then display the first form.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 46/80
Your First C# Windows Form
But don't worry if all that code has you scratching your
head. The thing to bear in mind here is that a method
called Main starts your program. And Program.cs in the
Solution Explorer on the right is where the code for
Main lives.
But we won't be writing code in the Program.cs file, so
we can close it. Have a look near the top of the coding
window, and you'll some tabs:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 47/80
Your First C# Windows Form
Right click the Program.cs tab and select Close from the
menu that appears. You should now see your form again
(you may have a Start tab as well. You can close this, if
you want).
To see the window where you'll write most
of your code, right click Form1.cs in the
Solution Explorer:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 48/80
Your First C# Windows Form
The menu has options for View Code and View
Designer. The Designer is the Form you can see at the
moment. Click View Code from the menu to see the
following window appear (you can also press the F7 key
on your keyboard):
F7 Key
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 49/80
Your First C# Windows Form
The code has a lot more using statements than before.
Don't worry about these for now. They just mean "using
some code that's already been written".
The code also says partial class Form1. It's partial
because some code is hidden from you. To see the rest
of it (which we don't need to alter), click the plus symbol
next to Form1.cs in the Solution Explorer:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 50/80
Your First C# Windows Form
Now double click Form1.Designer.cs. You'll see the
following code:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 51/80
Your First C# Windows Form
Again, you see partial class Form1, which is the rest
of the code. Click the plus symbol next to Windows
Form Designer generated code. You'll see the
following:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 52/80
Your First C# Windows Form
Initialize Component is code (a Method) that is
automatically generated for you when you
create a new Windows Application project. As
you add things like buttons and text boxes to
your form, more code will be added here for
you.
But you don't need to do anything in this
window, so you can right click the
Form1.Designer.cs tab at the top, and
click Close from the menu.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 53/80
Your First C# Windows Form
Click back on the Form1.cs tab at the top to see you
form again. If the tab is not there, right click Form1.cs in
the Solution Explorer on the right. From the menu,
select View Designer. Here's what you should be
looking at:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 54/80
Your First C# Windows Form
It's in Designer view that we'll be adding things like
buttons and text boxes to our form. But you can run this
program as it is. From the Debug menu at the top,
click Start Debugging (Or you can just press the F5 key
on your keyboard.):
When you click Start Debugging, Visual Studio will Build
the program first, and then run it, if it can. If it can't run
your program you'll see error messages.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 55/80
Your First C# Windows Form
From now on, when we say Run your
Program , this is what we mean: either press
F5, or click Debug > Start Debugging.
OK, time for you to start adding things to the
form, and to do a little coding!
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 56/80
Adding Controls to a Blank C# Form
The first thing we'll do is to add a button to the
blank form. We'll then write a single line of code, so
that you can see how things work.
If you want to add a control to a form, you can use
the Toolbox on the left of Visual Studio. Move your
mouse over to the Toolbox, and click the plus
symbol next to Common Controls. You should see
the following list of things that you can add to your
form:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 57/80
Adding Controls to a Blank C# Form
Click the Button item under the Common
Controls heading. This will select it. Now click once
anywhere on your form. A button will be drawn for
you, and your Form will look like this:
You can also hold down
your left mouse button
and drag out a button to
the size you want it
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 58/80
Adding Controls to a Blank C# Form
A button is something you want people to click on. When
they do, the code you write gets executed. The text on the
button, which defaults to "button1", can be changed. You
can add anything you like here, but it should be
something that's going to be useful for your users, such as
"Open a text file", or "Calculate Now".
We're going to display a simple message box when the
button is clicked. So we need to add some text to the
button. You do this by changing something called a
property. This is quite easy, and you'll see how to do it in
the next part of this lesson.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 59/80
Properties of a C# Control
The controls you add to a form have something
called Properties. A property of a control is things
like its Height, its Width, its Name, its Text, and a
whole lot more besides. To see what properties are
available for a button, make sure the button is
selected, as in the image below:
If a control is selected, it will have white squares Handle
surrounding it. If your button is not selected,
simply click it once.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 60/80
Properties of a C# Control
Now look in the bottom right of
Visual Studio, just below the
Solution Explorer. You should see
the Properties Window (if it's not
there, select it from the View menu
at the top. Again, 2010 users need
to click Tools > Settings > Expert
Settings to see the full list of menu
items.):
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 61/80
Properties of a C# Control
To view the list of Properties in As you can see, there's a lot of Properties
alphabetical order, click the AZ for a button. Scroll down to the bottom
symbol at the top, circled in red in the and locate the Text Property:
image below:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 62/80
Properties of a C# Control
The Text Property, as its name suggests,
is the Text you want to appear on the The Text part of your Properties
button. At the moment, it says button1. Window will then look like this:
Click inside of the text area of button1.
Delete the default text:
Now press the enter key on your
keyboard. Have a look at your Form,
and the Text on the button should
Now type the following: have changed:
A Message
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 63/80
Properties of a C# Control
There's a few more Properties we can Press the enter key again, and the
change before we get to the code. size of your button will change:
Locate Size in the Properties Window:
You can move your button around
The first number, 75, is the width of the the Form by clicking it with the left
button. The second number, 23, is the height mouse button to select it. Hold
of the button. The two numbers are separated down you left mouse button and
by a comma. Change the numbers to 100, 30: drag your button around the form.
Let go of your left mouse button
when you're happy with the new
location.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 64/80
Properties of a C# Control
Exercise
You can also move a button by changing
its Location property. Use the Properties
Window to change the Location of your
button to a position on the form of 100, 50.
(100 means 100 units from the left edge of
the form; 50 means 50 units down from the
top of the form.)
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 65/80
Properties of a C# Control
Exercise
A Form also has lots of Properties. Click away
from the button and on to the Form itself.
The Properties for the form will appear in the
Properties Window. Change the TextProperty
of the Form to A First Message.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 66/80
Properties of a C# Control
Exercise
The Form, like the button, also has a Size Property.
Change the Size of the Form to 300, 200.
After you complete the three exercises
above, your Form should look like this:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 67/80
Properties of a C# Control
When you changed the Text property of the Form,
you changed the text that runs across the blue bar
at the top, the text in white. You can type anything
you like here, but it should be something that
describes what the form is all about or what it does.
Often, you'll see the name of the software here, like
Microsoft Word, or Microsoft Visual Studio 2010
Express Edition.
We can now move on to some code, though, and
then run the Form to see what it all looks like.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 68/80
Adding C# Code to a Button
What we want to do now is to
display a message box whenever
the button is clicked. So we need
the coding window. To see the
code for the button, double click
the button you added to the
Form. When you do, the coding
window will open, and your
cursor will be flashing inside of
the button code. It will look like
this:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 69/80
Adding C# Code to a Button
The only difference from the last time you The _Click part after button1 is called
saw this screen is the addition of the code an Event. Other events are
for the button. This code: MouseDown, LocationChanged ,
TextChanged , and lots more. You'll
learn more about Events later.
After _Click, and in between a pair of
This is just another Method, a piece of code
round brackets, we have this:
that does something. The name of the
Method is button1_Click. It's called button1
because that's currently the Name of the
button. When you changed the Text , These two are know as arguments.
Location , and Size properties of the button, One arguments is called sender,
you could have also changed the Name and the other is called e. Again,
property from button1 (the default Name) to you'll learn more about arguments
something else. later, so don't worry about them
for now.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 70/80
Adding C# Code to a Button
Notice that there is a pair of curly brackets for
the button code:
If you want to write code for a button, it
needs to go between the two curly brackets.
We'll add a single line of code in the next part
below.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 71/80
A C# MessageBox
We want to display a message box,
with some text on it. This is quite easy Now type "ess" after the "M". IntelliSense
to do in C#. will jump down:
Position your cursor between the two
curly brackets. Then type a capital
letter "M". You'll see the IntelliSense
list appear:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 72/80
A C# MessageBox
The only options that start with Mess are all When you have MessageBox Selected ,
Message ones. The one we want is hit the enter key on your keyboard (or
MessageBox. You can either just type the rest, double click the entry on the list). The
or even easier is to press the down arrow on code will be added for you:
your keyboard to move down to MessageBox:
Now type a full stop (period) after the "x"
of MessageBox. The IntelliSense list will
appear again:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 73/80
A C# MessageBox
There are only three items on the list now, and As soon as you type the left round
all Methods (you can tell they are Methods bracket after the "w", you'll see all the
because they have the purple block icon next different ways that the Show method
to them) Double click on Show, and it will be can be used. There are 21 different
added to your C# code: ways in total. Fortunately, you don't
have to hunt through them all! Type
the following, after the left round
bracket (Don't forget the double
Because Show is a Method, we need some quotation marks.):
round brackets. The text for our message box "My First Message“
will go between the round brackets. So type a After the final double quote mark, type
left round bracket, just after the "w" of "Show": a right round bracket. Then finish off
the line by typing a semi-colon ( ; ),
Your coding window will then look like
this:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 74/80
A C# MessageBox
The text in the dark reddish color is what will be
Click your button to see your Message
displayed in your message box. To try it out, save
Box:
your work by clicking File from the menu bar at
the top of Visual Studio. From the File menu,
click Save All. You'll then see the same Save box
you saw for the Console Application. Save the
project.
Run your program by clicking Debug > Start
Debugging. Or just press the F5 key on your
keyboard. Your program will look like this:
Congratulations! It's your first
message! In the next part, we'll
explore other things you can do
with a message box.
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 75/80
More about the C# MessageBox
If you look at the message box we created in
the previous section, you'll notice there's no
Title in the blue area to the left of the red X -
it's blank You can add a Title quite easily.
Click OK on your Message Box. Then click the
Red X on your program to exit it. This will
return you to Visual C#. Go back to the coding
window (press F7 on your keyboard, if you
can't see it).
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 76/80
More about the C# MessageBox
Position your cursor after the final double Type the following:
quote of "My First Message", circled in red in "Message“
the image below: Again, you need the double quotes.
But your line of code should look like
this:
Now type a comma. As soon as you type a
comma, you'll see the list of Show options
again: When your line of code looks like the one
above, Run your program again. Click your
button and you should see a Title on your
Message Box:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 77/80
More about the C# MessageBox
Rather than having just an OK button, The one that adds buttons to a message box
you can add buttons like Yes, No, and is, you won't be surprised to hear ,
Cancel to your C# message boxes. We'll MessageBoxButtons . Press the enter key
add a Yes and a No button. on your keyboard when this option is
highlighted. It will be added to the your
code. Now type a full stop (period) after the
final "s" of MessageBoxButtons . You'll see
the button options:
Double click the one for YesNo, and it will be added to
your code.
Run your program again, and click your button. Your
Message Box will then look like this:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 78/80
More about the C# MessageBox
Another thing you can add to brighten up your We've gone for Asterisk. Double
Message Box is an Icon. It's easier to see what these click this to add it to your code.
are than to explain! Run your program again to see
Type another comma what the icon looks like on your
after MessageBoxButtons.YesNo. After the comma, Message Box:
type a capital letter "M" again. From the IntelliSense
list that appears, double click MessageBoxIcon. After
MessageBoxIcon, type a full stop to see the available
icons:
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 79/80
More about the C# MessageBox
Looks pretty impressive, hey! And all that with one line of code!
We'll move on to the important subject of variable, in the next
part. First, try this Exercise.
Exercise
Try the other icons on the IntelliSense list, and see what
they look like when your program runs. Does the
Information icon differ from the Asterisk? (To quickly
display the IntelliSense list again, delete the word
Asterisk from your code, then delete the full stop. Type
the full stop again, and the IntelliSense list will
reappear.)
Oct. 2011 C# Tutorial by M. Abdollahi http://www.abdollahi.us 80/80