How To Create Your First Application

03myersd edited this page Nov 10, 2010 · 1 revision

How To Create Your First Application

Creating A New Project

  1. Open SharpDevelop and select new Solution from the file menu.

  2. From the new project window, select a language on the left (this tutorial will be using C#) and choose a project type and template. I will be choosing a console application from the Windows Applications category.

  3. In the name field put "Hello World" and click Create.

SharpDevelop will now generate the template code needed for your application. It is assumed that you understand how to program, and so it will not be explained what each line of code does.

  1. At the top of the screen click the green triangle to run the program.

Adding Files

We are now going to take this Hello World program and start building upon it. Let's give it a message displaying class as an example.

  1. Add a new class to the project by selecting the project tool bar button and Add a New Item. Select the Class template and give it a name along the lines of messageDisplay.

  2. Change the class code to this:

    public class messageDisplay { public String messageToDisplay = "";

     public messageDisplay(String message)
     {
     	messageToDisplay = message;			
     }
     	
     public void display()
     {
     	Console.WriteLine();
     }
     
     public void waitKeyContinue()
     {
     	Console.Write("Press any key to continue . . . ");
     	Console.ReadKey(true);
     }
    

    }

And the main function to:

public static void Main(string[] args)
{
	messageDisplay mDis = new messageDisplay("Hello World!");
	mDis.display();
	mDis.waitKeyContinue();
}

Running the program will do the same output as before.

Many other files can be added in the same way.

Form Designer

In order to use forms its best that we change our project type. Do the same as above but instead of choosing Console Application, choose Windows Application. This will give us a project with a form already included.

To open the forms designer click the Design tab at the bottom of the form's source code window.

Adding Controls

Controls can be added to the form from the Toolbox window. From the View menu select Tools.

Click a tab to expand it and see what controls it contains. To add a control to the form you can either drag it or draw it onto the form. To drag, click the control with the left mouse button and whilst holding down the left mouse button move the mouse to your form, release the mouse button to drop the control onto your form. To draw it onto your form, select the control in the toolbox window by left clicking it with the mouse, no need to hold the button down at this point, then click where you want the top left corner of the control to be located on the form, then whilst holding down the left mouse button drag to where the bottom right corner of the control should be located. Once the control is placed on the form you can visually resize it, change its location and modify its properties. In the screenshot below a Button, TextBox and Timer have been added to the form, with the Timer shown in the non-visual component area of the forms designer.

Setting Properties

A control has properties that can be set at design time, changing how the control looks and reacts (e.g. text displayed on a button, font used to display the text, and the background image on a button). These properties are displayed in the Properties window. From the View menu select Properties to open the Properties window.

The properties are then displayed for the currently selected control. You can view the properties of another control on the form by selecting it in the designer or by selecting its name in the drop down list at the top of the Properties window.

Events

Each Windows Form control has events that you can catch and respond to. To see the events a control exposes, select the control in the designer and select the Events tab Form Events tab in the Properties window.

To generate an event handler, select the event in the Properties window, enter a name, or leave it blank to accept the default name, and then double click the property or press the enter key. The forms designer will then create an empty event handler method and switch to the form's source code ready for you to type in your code. The screenshot below shows the code generated for the button's Click event.