The important things will be red so you know exactly what to look for if you don't want to read through this whole post.
The first thing we need to do is download and install C#. Follow this link to get the free Microsoft Visual C# 2010 Express. Select your language and run the file you downloaded (something like vcs_web.exe). Now, just follow the instructions on the screen. Installing it and updating your .NET framework may take a little bit of time, so go have a snack. Hang out with your friends. Play a video game.
When it's finally done installing (maybe even after restarting your computer for the .NET framework), it probably made a shortcut to C# on your desktop. If it didn't, go into your Start Menu > Programs > Microsoft Visual Studio 2010 Express. So, open C#.
It might say it's configuring for your first time. Again, you have to wait. But not that long, so go get a drink of water or something.
When that's done, click the "New Project" button right under the File menu. In the window that pops up, select Windows Forms Application, which should be the default. Name it "HelloWorld" and hit "OK". The window-like thing you see in the middle of your screen is called a "Form" which houses everything you are going to put on it.
Let's keep it simple and create a simple program that displays "Hello World" when a button is clicked. Click on your form and go to the Properties window in the bottom-right of your screen. These are values that change how your Form looks. First, lets change the "Name" property. This is how you will reference your Form in your code. Change the Name field to "frmHelloWorld" (I use the frm prefix for Forms). Now, lets change the "Text" property, which is what will be displayed on the title bar. You can put whatever you like here.
Now that we set up our form, drag a "Button" control from the Toolbox at the right side of your screen onto your Form. Again, we'll rename this. Let's name the button "btnMessage" (I use the btn prefix for Buttons). You can change the "Text" property to whatever you want. Whatever you choose, it'll be displayed on the button.
To run your program now, click the forward-pointing green arrow (or press F5 on the keyboard) to debug. Your Form will appear. You can move it around. You can resize it. You can click the button on it (but nothing happens). When you're done, close the form to get back to editing your program.
Although your Form can actually be seen and moved around, it doesn't actually do anything. Now we need to add an event, which is how your program will interact with your user. Double-click the button on your Form (when you're back in the editor). This will add a "Click" event to your form. Now you're looking at actual code. Whenever your button is clicked, it will run whatever is between the brackets after "private static void btnMessage_Click...". We will add a simple line of code:
MessageBox.Show("Hello World");
You DO need the semicolon at the end because it means that you're ending a line of code. If you didn't already guess, this creates a Message Box that pops up with the message "Hello World" when you click on the button. (I don't know why programmers are so obsessed with "Hello World"... maybe if you wanted to be a rebel, you could say "Greetings Earth")
Run the program again, and click the button. If "Hello World" shows up, congratulations, you have created your first program!
If you're still interested, you should play around with the items in the Toolbox or change the things' properties to see what they do. If not, you should still give yourself a pat on the back because you took the first step in starting to program!
If you're still interested, you should play around with the items in the Toolbox or change the things' properties to see what they do. If not, you should still give yourself a pat on the back because you took the first step in starting to program!
No comments:
Post a Comment