Skip to navigation | Skip to content

Share your knowledge. Make a difference.

Creating Your First C# Program

1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic (by 2 people)   Your rating: 1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic

Ranked #2380 in Tech, #54449 overall

Donates to The Leukemia & Lymphoma Society

Rated G. (Control what you see)

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 

The first thing any C# book or class will tell you before you dive in is how great C# and the .NET framework are. Personally, this is a waste to me, since I've already made the decision at that point to learn the language anyway. So I'm going to skip over the benefits of the .NET and only briefly describe how it all works. It's important, but it won't really make you a better programmer at this stage of your development.

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 

If you're using Visual Studio as your IDE (integrated development environment), then there are a couple things that may be helpful to you. First off, there's IntelliSense. IntelliSense is one of the main reasons to use an IDE. It's a convenient way to auto-complete variables and access descriptions of methods and there parameters. To use IntelliSense, use Ctrl + Space Bar while you're typing either a variable, method, class. It'll pop up with a drop down that has options matching the letters you've typed thus far.

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 

Here's an overview of the coding process. While the bulk of the tutorials will focus on step four, all aspects of the development process will be touched upon, and you should be able to deploy a setup program for your application by the end of the series. Creating an application involves seven steps:
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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 

Form1 already has some basic elements for us: a title bar, minimize, maximize, and close. Isn't that nice? If you double click the form it'll take you to the code view and you can see all the stuff that has been preset up for us by choosing the Windows Application template. You can get by to the forms designer by clicking the Form1.cs[Design] tab at the top or pressing Shift + F7.

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 

Loading Fetching RSS feed... please stand by
X
ScratchProjects

About ScratchProjects

Hi, my name is Kevin. I'm a professional C# programmer and a hobbiest in PHP. I also run the site ScratchProjects.com, so that others can learn to program from more interesting tutorials and projects.

ScratchProjects's Pages

See all of ScratchProjects's pages