00_GettingStartedwithCSharpForms

advertisement
Getting Started with C#
When you open C# you will see the Visual C# 2010 Express start page.
To start a new project click “New Project…”
You will then need to select the
type of program you want to
create. We will be creating
“Windows Forms Application”
programs.
Give your program a name in the
text box at the bottom and click
“OK”.
Make sure you use proper
naming conventions.
Designing Your Program
Your program will open a new form called Form1. This is where you will be designing
your program.
The first thing we want to do when we start a new program is to name our form. To do
this we need to open the Properties window. Go to View > Other Windows > Properties
Window, or use the shortcut Alt + Enter.
Make sure the form is selected in the design window and that Form1 appears in the drop
down list in the properties window.
Scroll up to the top to find the property (Name) and change the
name of your form to HelloWorld.
Find the text property and change Form1 to My First Program.
What does this do? ________________________________
Note: Follow naming conventions for all objects.
From the tools menu (on the far left of your screen) drag a Button onto your form.
In the Properties Window name your button (using proper naming conventions. Eg.
ClickMe) and change the text on the button.
Writing Some Code:
This is all we are going to have on our form for now. Now we need to make our form do
something. For this exercise we are going to make a pop-up message box that will say
“Hello World”.
(Hello World is a standard first program that almost every programmer has created –
and often a quick program to try and figure out the basics of new programming
languages).
We want the pop up box to open when the button is clicked, so to add the code double
click the Button on your form.
You will see the code window open as a tab in your main window.
We will talk about everything in this screen in more detail; however, for right now we
want the pop up to happen when the button named (in my example) ClickMe is clicked.
We need to write the code “inside” the method named ClickMe_Click. All the code that
goes in this method goes between the braces { } that follow the method line.
In C# to create a message box we simply write:
MessageBox.Show("Hello World!");
CAPITALS MATTER !
PUNCTUATION MATTERS !
Then click the run button, or
press F5.
Download