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.
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
{
}
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;
}
}
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 Once you know where the CSC compiler is located, open the Command Prompt. To do that:
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:
After typing it, press Enter:
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:
n the next window, click System:
If you are using Microsoft Windows XP, click Environment Variables:
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:
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.
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
After compiling, to execute, type the name of the file that contains the Main() function and press Enter.
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. 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||