Home

Introduction to C# Windows Programming

 

Windows Programming Fundamentals

 

Introduction

Microsoft Windows is an operating system that provides graphical objects used by people to interact with a computer. These objects, called windows, must be primarily created by a person or a company, then installed on a computer that will be used. To create one of these objects, you can use a computer language such as C++, Java, Object Pascal, Assembly, C#, etc. You can also use a programming environment to do this.

One object used on the computer is usually not enough to make it particularly useful. Another approach consists of grouping various objects into an ensemble called a computer application, or a Windows application, or a computer program, or a program, or an application. All these words or expressions will be used interchangeably on this site but they mean the same thing.

C# and Windows Programming

The C# language provides almost everything you need to create a computer application. To create a typical program, you will use your knowledge of C# and additional new concepts we will introduce.

A Windows application primarily appears as a rectangular object that occupies a portion of the screen. This type of object is under the management of the operating system: Microsoft Windows. Based on the functionality of Microsoft Windows, for an application to become useful, it must be opened. An application must have an entry point. On a C/C++ application, this entry point is a function called main. On a Win32 application, this entry point is a function called WinMain. The C# language defines this entry point with a function called Main, as you should know already from your learning C#.

The Main() method of C# can be defined as void or as returning an integer value.

Practical LearningPractical Learning: Introducing Windows Applications

  1. Start Notepad
  2. From what we learned about C#, in the empty file, type the following:
    using System;
    
    namespace WindowsFundamentals
    {
      class Exercise
      {
        static int Main()
        {
    	return 0;
        }
      }
    }
  3. To save the file, on the main menu, click File -> Save
  4. If you are working on your own computer, locate the C: drive and display in the Save In combo box. If you don't have access to the C drive or will not be allowed to create a folder on the C: drive, locate your My Documents folder and display it in the Save In combo box
  5. Click the Create New Folder button
  6. Type CSharpWindows and press Enter twice to display it in the Save In combo box
  7. Click the Create New Folder button again
  8. Type Fundamentals1 and press Enter twice to display it in the Save In combo box
  9. Set the File Name to Exercise.cs and change the Files of Types to All Files
     
  10. Click Save

Applications Fundamentals

 

Introduction

Windows Forms is a technique of creating computer applications based on the common language runtime (CLR). It offers a series of objects called Windows Controls or simply, controls. These controls are already created in the .NET Framework through various classes. Application programming consists of taking advantage of these controls and customizing them for a particular application. To exploit these controls and other features of the .NET Framework, there are various types of applications you can create, including graphical applications (windows), web-based applications (ASP.NET web applications), or console applications, etc.

There are two broad categories of objects used in a Windows Forms application: the forms and the controls. The objects used in a Windows Forms application are stored in libraries also called assemblies. As normal libraries, 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. Then you must add a reference to that assembly in your application.

To add a reference to an assembly, when compiling the program, type either /reference: or /r: followed by the name of the assembly.

Forms Fundamentals

A form is the most fundamental object used on an application. It is a rectangular object that uses part of the computer desktop to represent an application. A form is based on the Form class 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. There are various techniques you can use to get a form in your application: you can programmatically and manually create a form, you can inherit a form from the Form class, you can create a form based on another form that either you or someone else created already, etc.

The primary means of getting a form into an application consists of deriving one from the Form class.

When compiling a program, to indicate that you want to create a Windows executable application, add the /target:winexe switch.

 

Practical LearningPractical Learning: Deriving a Form From the Form Class

  1. To inherit a form from the Form class, change the file in Notepad as follows:
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFundamentals
    {
      class Exercise : Form
      {
        static int Main()
        {
    	return 0;
        }
      }
    }
  2. Save the file

 

The Application Class

The form is the object that gives presence to an application. Once you have created the (primary) form of your application, 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, main or primary form of your application; it will be the first to display when the application comes up.

Practical LearningPractical Learning: Starting an Application

  1. To prepare the application for starting, change the Main() method as follows:
     
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFundamentals
    {
      class Exercise : Form
      {
        static int Main()
        {
    	// Instantiate an Exercise object
    	Exercise frmMain;
    
    	// Allocate memory for the object, using the new operator
    	frmMain = new Exercise();
    	// 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. Save the file and close Notepad
  3. Open the Command Prompt and switch to the folder that contains the above Exercise.cs file
  4. To create the application, type
     
    csc /reference:System.Windows.Forms.dll /target:winexe Exercise.cs
  5. Press Enter
     
  6. Open Windows Explorer or My Computer and locate the folder that contains the current exercise
     
  7. To execute the application, double-click Exercise
     
    A Simple Form
  8. Close it by clicking its system Close button Close
 

Copyright © 2004-2012, FunctionX Next