Introduction: Your First Program in C#
Make a basic program that opens a messagebox and then start modifying it to make it your own!
You will need
- A computer
- Microsoft Visual C# Express edition (Go here if you dont have this, its free! http://www.microsoft.com/express/vcsharp/)
- To want to program or learn something about programming.
Step 1: Creating the Program
1. Open Microsoft Visual C# Express
2. Click on the " Create : Project" button on the upper leaft hand corner of the screen
3. Select the " Windows forms Application" Icon
4. Type the name of your new program into the box (I name mine "My First Program" so I would know that it was for this Instructable)
5. hit enter or click the "OK" button
Step 2: Your Developer
Your screen should look like the picture below now. The Center Panel is where your form is (This is what you will see when the time comes to run your program). Depending on how your setting are, you will have the Solution Explorer as your top right Panel, and the Properties as the bottom right panel.
We will be using these windows, and if either of them are not there, go to the View menu, and select the "Solution Explorer" option if that is the window you are missing, or the "Properties" button if you are missing your properties Panel.
Another Panel we will be using is the Toolbox. This should be located on the left side of your screen, as a tab that says "Toolbox". Click on it and there should be your toolbox. If you do not have that tab, got to View (same as before) and click on the toolbox option.
Step 3: Start Designing the Form
1. Click on the form (This should select it, allowing you to edit it's properties in the Properties panel)
2. Search through the "Properties" panel until you find the "Text" selection.
3. To the right of the word "Text" there should be a box that says "form1" double click on it
4. Type in this box what you would like the top of your form to say (I chose to type "My First Program" since that is the name I chose in the making of the project)
5. Open your toolbox
6. Search for the "Button" control in the "Common Controls" section of your toolbox
7. Drag this onto your form
8. In the "Properties" panel go to the "Text" selection again but this time, type "Open!"
9. Click on the form
10. Click and drag the lower right hand corner to resize the window to the size you would like
Step 4: The Code Editor
In the next step you will be working with code. Your code editor will look different from mine because I changed my setting so that text is bigger and the background color is black, and that most text is yellow (This is just a personal prefence to help my eyes)
This is what my code editor looks like when I double click on the button on our form.
Yours will have a white background and black text (if I recall correctly)
Step 5: Program the Button Control
in this step you will write your first line of code! In the first part of this step I will just tell you what to do, and after that step I will tell you what you just did.
1. Double click the button control on your form
2. Click between the two brackets under "private void button1_Click(object sender, EventArgs e)"
3. Type in " Messagebox.Show("Hello World"); "
4. Make sure to save
5. A window will come up when you save, just click the "Save" button
Now for the explanation:
1. When you double clicked the button Microsoft Visual C# just created this line of code for you "private void button1_Click(object sender, EventArgs e)". Essentialy saying that when the program is running, follow the instruction between those brackets.
2. You were just selecting the place where you are going to put your instructions
3. Here you types your instructions. The "Messagebox" portion of this code was saying that the following code was for a messagebox. When you typed "Show' you were telling the program to open the messagebox you are talking about. After that, the ("Hello World") was what you wanted the messagebox to say.
So, altogether, you told your program to open a box that says "Hello World!".
The semi-colon tell the program that "This line of code is over, move one" Always end your line of code witha semi-colon
Step 6: Test Your Program!
Here, you finally get to run your brand new, fresh out of the oven, program!
1. All you have to do is press "F5" on your keyboard.
2. Your "Form1" will open
3. Click the button
4. If you have doen everything correctly, then a messagebox will appear saying " Hello World!" If not then it is alright, that is why we run tests! To make sure it works. Continue one to debugging if it didnt work.
Step 7: Debugging (Skip If Your Progam Doesn't Have Bugs)
Debugging is every programmers nightmare. It can be a long grueling process, and sometimes, you can't fix your program. Lucky for you, this is a small program, and not so difficult!
If you ran your program and an error message occured saying that a certain character was expected, add that character ( It might have been accidently been deleted) Then try to run your program. If ou get another error message, then you would be safer to just undo adding that character. Review all of the steps of this instructable and see where you strayed from the path!
Check to make sure you put a semi-colon at the end of your line of code!!! This is the most common reason for problems with programs.
As a failsafe go throught this instructable again and check to see where you made a mistake (we are all human)
Use the 'Help" Menu The "How Do I" section is helpful, and if you were given a error then you can search it here, or just search some of the key words (Look at the pictures below)
If you can't figure it out, then either send me a private message, or submit a question a forum of programmers.
Step 8: Publish (Make the Install Package)
For this small program, this is not vital, but if you want to be able to run your program outside of the IDE (The program you are using to make programs) then you need to publish it.
1. Click on the "Build" menu, and select "Build My First Program" (It will say whatever it is that you named your project)
2. Go throught the setup, and when the step come to choose where to put the files, make sure it's someplace you will remember so that you don't loose your program) Other than that just use the defaults.
Step 9: Make It Your Own!
Now that you can do some of the basics, try new things, add new controls and try to connect them! You can do anything here, and if you are able to look at this instructable, then you can find out how to do anything.
Good Luck, and Thanks For Reading!
We have a be nice policy.
Please be positive and constructive.