Home

Graphical Applications Fundamentals

     

A Project

 

Introduction

A computer program or application is a series of files such as code files, assemblies (or libraries), and resource files.

The file that holds code for a C# application has the extension .cs. After creating the file, save it in an appropriate folder.

Author Note To follow these lessons, you should already know the C# language. You are not supposed to have any prior knowledge of graphical application programming.
 

We are going to learn how to create graphical applications, also called Windows applications, or Windows Forms applications. An application must have an entry point. This is done using a function named Main.

Windows Fundamentals

A graphical computer application starts from an object named a form. To assist you with creating such an application, Microsoft developed a library named the .NET Framework. It uses a technique of creating computer applications based on the common language runtime (CLR) from a series of objects called Windows Controls or simply, controls.

The objects used in a Windows application are stored in libraries called assemblies. These assemblies have the extension .dll (which stands for dynamic link library). In order to use one of these objects, you must know the name of the assembly in which it is stored.

Forms Fundamentals

There are two categories of objects used in a Windows application: the forms and the controls. A form is the most fundamental object used in an application. It is a rectangular object that uses part of the computer screen to represent an application.

A form is based on a class named Form that is defined in the System.Windows.Forms namespace created in the System.Windows.Forms.dll assembly. Every GUI application you will create starts with a form. To create it, you must create a class that inherits  from the Form class. Here is an example:

class Exercise : System.Windows.Forms.Form
{

}

ApplicationTopic Applied: Deriving a Form From the Form Class

  1. Start Notepad
  2. To inherit a form from the Form class, type the following:
    using System;
    using System.Windows.Forms;
    
    public class Starter : Form
    {
        public static int Main()
        {
            return 0;
        }
    }
  3. To save the file, on the main menu, click File -> Save
  4. Locate the root of the C: drive and display it in the top combo box
  5. Click New Folder
  6. Type Exercise1 and press Enter
  7. Double-click the new Exercise1 folder to display it in the top combo box
  8. Change the name of the file to "Exercise.cs"
  9. Click Save

The Application Class

After creating a form, you can get it ready to display on the screen. This is taken care of by the Application class equipped to start an application, process its messages or other related issues, and stop the application.

The Application class provides the overloaded Run() method that can be used to start a program. One of the versions of this method takes a form as argument. This form must be the first or primary form of your application; it will be the first to display when the application comes up. Here is an example of calling the Application.Run() method:

using System;
using System.Windows.Forms;

public class Exercise : Form
{
    public static int Main()
    {
        Application.Run(new Exercise());

        return 0;
    }

}

The CSC Compiler

After writing code, to build the project at the Command Prompt, you use the csc.exe compiler (csc stands for C# compiler). The csc compiler is free from Microsoft. You likely have it already on your computer. If not, download the .NET Framework from the Microsoft web site. Find out where your .NET Framework folder is because that folder contains the csc compiler. By default, its path is C:\Windows\Microsoft.NET\Framework\v4.0.30319

The csc Compiler

Once you know where the CSC compiler is located, open the Command Prompt. To do that:

  • If you are using Microsoft Windows XP, Vista, or 7, click Start -> (All) Programs -> Accessories -> Command Prompt
  • You can also issue a command for the Command Prompt:
    • If you are using Microsoft Windows XP, click Start and click Run...
    • If you are using Microsoft Windows Vista or 7, click Start
    In the text box, type cmd and press Enter

Building a Project

To compile a code file in order to build the project, at the Command Prompt, switch to the folder that has the code file. Type the (complete) path to the csc application, followed by the name of the file with its extension. Here is an example:

Command Prompt

After typing it, press Enter:

Command Prompt

To build a project, instead of typing the whole name to the csc application, you can add the path of the csc.exe to the Environment Variables's Path, but you must add admin rights on the computer (if you are programming from work, you may not have those rights. In that case, you can either use the technique we mentioned above or you can request admin rights from your help desk (or network administrator) but then you have to justify blah blah blah; in most cases, you don't need that headache). To start, select the path in the top combo box of the file utility and copy it (to the clipboard). Open the Control Panel. From Control Panel, click System and Security:

System

n the next window, click System:

System

If you are using Microsoft Windows XP, click Environment Variables:

System Properties

If you are using Microsoft Windows Vista or 7, in the next window, click Change Settings. In the System Properties dialog box, click the Advanced property page, then click Environment Variables.

If you don't have admin rights on the computer, the bottom New, Edit, and Delete butons would be disabled:

Environment Variables

If the bottom New button is disabled, click Cancel. Otherwise, under System Variables, click Path. Click Edit. First check whether the path had been added already. If it doesn't exist, press the right arrow key, type a semi-colon, then paste the path. Click OK, OK, and OK.

Before building the project at the Command Prompt, switch to the folder that contains your file. To compile, type csc followed by the name of the file and its extension. To execute, type the name of the file that contains the Main() function and press Enter.

Using an Assembly

If your project is using an assembly (DLL), when compiling, you must add a reference to that assembly. To do this, type csc or its path, followed by either /r: or /reference:, followed by the names of the DLLs that contain the objects you are using in your code, followed by the name(s) of the file(s) that contain(s) your code and its (their) extension(s). An example would be:

csc /reference:System.Windows.Forms.dll exercise.cs

To indicate that you are creating a graphical application, after the list of DLLs but before the name(s) of the file(s), type either /t:winexe or /target:winexe. Here is an example:

csc /reference:System.Windows.Forms.dll /target:winexe exercise.cs

ApplicationTopic Applied: Accessing the CSC Compiler

  1. If you have admin rights on the computer, open Windows Explorer
  2. Navigate to the C:\Windows\Microsoft.NET\Framework folder
  3. Locate and double-click the latest version of the .NET Framework
  4. Select the path in the top combo box copy it
  5. Open the Control Panel
  6. From the Control Panel, click System and Security:
     
    System
  7. Click System
     
    System
  8. Click Change Settings
  9. In the System Properties dialog box, click Advanced
  10. Click Environment Variables
  11. Under System Variables, click Path
  12. Click Edit
  13. Check whether the path had been added already. If it doesn't exist, press the right arrow key, type a semi-colon, and paste the path. Click OK, OK, and OK

Executing a Project

After compiling, to execute, type the name of the file that contains the Main() function and press Enter.

ApplicationTopic Applied: Building a Project

  1. To prepare the application for starting, change the Main() method as follows:
    using System;
    using System.Windows.Forms;
    
    public class Starter : Form
    {
        public static int Main()
        {
            // Instantiate an Program object
            Starter frmMain;
    
            // Allocate memory for the object, using the new operator
            frmMain = new Starter();
    
            // Call the Run() static method of the Application
            // and pass it the instance of the class to display
            Application.Run(frmMain);
    
            // Everything went alright... We hope
            return 0;
        }
    }
  2. Press Ctrl + S to save the file
  3. Start the Command Prompt (Start -> (All) Programs -> Accessories -> Command Prompt)
  4. To switch to the root directory, type CD\ and press Enter
  5. To switch to the folder that contains the file, type CD Exercise1 and press Enter
  6. To build the project:
    • If you have admin rights on the computer and if you had changed the path in the Environment Variables, type the following:
      csc /r:System.Windows.Forms.dll /target:winexe Exercise.cs
    • If you don't have admin rights on the computer and you had not changed the path in the Environment Variables, type the following:
      C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc /r:System.Windows.Forms.dll /target:winexe Exercise.cs
  7. Press Enter
     
    Command Prompt
  8. To execute, type Exercise and press Enter
     
  9. To close the form, click its system Close button
  10. To close the Command Prompt, get to it and type exit
  11. Press Enter

Opening a Code File

As opposed to creating a new code file, you can open one. To open an existing file, launch Notepad. On the main menu, click File -> Open ... This action would display the Open File dialog box. This allows you to select a file and open it.

Regions

A region is a section that delimits code in a logical manner. Regions have no importance in your code. If you want to have regions, you must create them.

A region must have a beginning and an end. To specify the start of a section, type #region. You can optionally add a label to the right of #region to name the region. If you don't specify the end of the region, the code from #region to the end of the file would be considered as belonging to the to the region. Therefore, you should specify the end of the region you created. To mark the end of the region, in the desired line, type #endregion.

 

Home Copyright © 2011 FunctionX Next