Creating Your First C# Program
C# is a great programming language. Not only can you make powerful applications, but you can make them extremely quickly. Dive in and learn how to create your first C# application.
A Very Brief Intro to the .NET Framework
Basically the .NET Framework is divided into two sections: the class library (which makes your programming a lot faster) and the common language runtime (which handles the execution of your code). When you compile the application, your code is translated into an intermediate language called MSIL (Microsoft Intermediate Language). All .NET compatible languages are first translated to MSIL so that you can use code from multiple languages within the same program.
Customizing Visual Studio
Something else I like to do is change the color of my strings in order to help distinguish them within my code. To do this go to Tools >> Options >> Fonts and Colors. From there you can change the color of any element within Visual Studio.
The Stages of the Development Process
- Create a design specification
Before you write any code, it helps to take a little time to draw up your design ideas. Be sure you run the design by the intended users to make sure that you didn't leave anything out. Having a blueprint will save you tons of time during your actual coding. - Create the user interface
Creating the interface in Visual Studio is done by dragging items from the Toolbox into a Windows form. When making the user interface it's a good idea to use standard conventions so that way the users should be able to navigate the program without any training. - Set the properties for the user interface objects
Using the Properties window or the code editor you'll set the properties for each object you added to the form. - Write code to add functionality
After you've got everything looking pretty, you begin the fun task of adding the functionality that will run in response to an event. Examples of an event are: clicking a button, changing a field, or even moving the mouse. Pick the events you want to respond to and program accordingly. - Test and debug
A lot more time is spent here then you would probably expect, even for expert programmers. Anytime you make a change to the program, be sure to run some test cases. - Create the setup application
Finally, Visual Studio provides a Setup Wizard that will automatically create a program that will include all libraries (DLLs) needed to run the program.
Creating a Windows Application Project
After opening up Visual Studio and selecting create a new project (File >> New >> Project), you'll see a number of options.These options are called application templates. Each application template provides different starter files and project structure that are most common when building that type of application. We'll be focusing on Windows Applications for now, but we'll probably dive into ASP.NET Web Applications, and possibly even ASP.NET Web Services a little later.
Name is obviously the name of the project and location is where your code is going to be save, not where it will be installed. Click OK and you should see Form1. Technically this is a program; you could build it, run it, and distribute it. But everyone would make fun of you, so let's keep on going.
Using the Forms Designer
On the left side of the screen you should see the Toolbox, if not go to View >> Toolbox. The Toolbox contains a variety of controls that you can use to add images, labels, buttons, list boxes, etc. If you want to close the Toolbox, click the X next to it. I prefer just to hide it by unpinning it (clicking the thumbtack). Then you'll have it in a little tab on the left side of the screen. From the Toolbox drag and drop a textbox and a button onto your form.
If you select the textbox that's on your form, the Properties window for that object should appear in the lower right corner of the screen, if not go to View >> Properties Window. The Properties window lists the property settings for the selected form or control that you can modify while you create or edit an application. A property describes a characteristic of an object such as name, position, or size.
Set the following properties for the newly created textbox:
Location: 100, 50
Name: txtInputBox
Text: Leave this blank
Set the following properties for the newly created button:
Location: 200, 225
Name: btnSubmit
Text: Submit
If you double click the Submit button, it'll take you to the code view and you'll notice that a new method called btnSubmit_Click has been automatically created. Anytime the Submit button is clicked from within the program the code between the { } braces will be executed. Between those braces type the following code:
MessageBox.Show(txtInputBox.Text);
Running the Application
At this point we're ready to run our first piece of code. You can either go to Debug >> Start or you can simply press F5. Form1 should appear. Type something into our text field and then hit the Submit button.
A window should appear displaying whatever you typed into the text field. Beats "Hello World", doesn't it? Latest Releases From Scratch Projects
Fetching RSS feed... please stand by
(by 2 people)
