. NET platform Introduction to Windows Forms Programming-1

Source: Internet
Author: User
Tags garbage collection new features web services visual studio
Introduction:

Microsoft's. NET platform is a new generation of Internet platform. For technical developers,. The two features of the net platform are remarkable, first of all, the common language runtime, the common language runtime platform, and the large and comprehensive unified programming class.

The C # language is a Microsoft company target. NET platform to launch a new language, as. NET platform, which concentrates almost all the latest results on software development and software engineering research. It not only inherits the C + +, Java and other object-oriented powerful features, but also inherits the VB, Delphi and other programming languages of the rapid development of visual function, it is currently the first fully oriented components of the language.

. NET platform is divided into the following four aspects: Web Forms, Windows Forms, Web service, and Windows service, so let me introduce you to the design of the Windows Forms program.

1. Essentials of Basic knowledge

C # language: C # language has evolved from C + +. It is modern, simple, fully object-oriented and type-safe. If you are a C/s + + programmer, the learning curve will be very flat. Many C # statements borrow directly from your favorite language, including expressions and operators. C # is a modern programming language. C # reduces some of the features of C + +, with no more macros, templates, and multiple inheritance. New features that make programming more convenient are strict type safety, versioning, garbage collection (garbage collect), and so on. All of these features aim to target the development of component-oriented software. C # is therefore also referred to as a component-oriented language.

Class: A class is a set of objects that have the same data structure and the same operations. A class is an abstraction of a series of objects of the same nature, a description of the common characteristics of objects. For example, every car is an object, all cars can be used as a template, we define the car this class. C # is object-oriented, and all C # code must be placed in a class, with no global variables and global names.

. NET class Library: Microsoft has designed a very large programming class for the. NET platform. These classes range from the bottom of the operating system to the Windows interface, from database development to Web services, and so on. These classes are both on the common language runtime and are administrative, secure code. by using. NET provides a "super" class library that you can easily develop. NET platform for four types of programs: Web Forms, Windows Forms, Web service, and Windows service.

Windows Forms programs: Similar to Windows programs, programs that run on platforms such as Windows 98,windows NT and Windows2000, with very beautiful faces (such as forms, menus, various controls, and so on). Windows Forms is the first concept that appears in the. NET platform, also known as Windows Forms, and because it is a. NET Windows forms program, your Windows system must be installed first. NET platform in order to run.

Events: In a Windows system, all of our actions on a form will raise an event, such as pressing a button to raise a click (click), which will raise the text change event of the text box when you enter content in the text box. When an event is generated, a message is sent and the message is translated and processed by a specialized message handler.

Visual Studio. NET: A powerful software development tool that integrates multiple development languages on the Microsoft. NET Platform. Through this development tool, most. NET programming languages can be implemented in RAD (rapid development).
2 several basic concepts in Windows forms

Start Program

The Windows forms program differs from the general application in that it interacts with the user through a very beautiful form of appearance and various controls on the form. A program is not a step-by-step execution, but a message loop that chooses to perform the function according to the events raised by the user's various actions. In order to manage, interpret various messages for Windows, user keyboard or mouse input, and respond to various events. NET class library provides us with a class dedicated to starting Windows programs: System.Windows.Forms.Application, by using the application class, we can easily start a Windows Forms program, And the interpretation of the message, keyboard and mouse input management, the response to all kinds of events are entrusted to application responsible for processing.

In addition, in a Windows application, as with a general application, the entry of a program is also the main () method in a class (note that main requires a first letter capitalization), and the same must be a static (static) method, otherwise the program will not find the entry point.

Here's a very simple example of starting a Windows program in C #:

static public void Main ()

{
Application.Run (New Form ());

}

In addition, the end of the Windows forms program is also performed by the application class.

Form

The most important success of the Windows operating system stems from its beautiful appearance, that is, the form. General Windows programs have a form where you can place a wide variety of controls (such as tags, text boxes, command buttons, and so on) on a form, forming a human-computer interface. Using a form in. Net Windows forms simply creates an object from the System.Windows.Forms.Form class or from its derived class.

The following code is an example of a very simple inherited form (form) class in C #:

public class MyForm:System.Windows.Forms

{

Private System.Windows.Forms.Button button1;

Public MyForm ()

{

}

}

A button (Button) object is added to the custom form class MyForm.
Event

In Windows Forms programs, most of the user's actions will result in Windows events, such as pressing a button to raise a click (click), the user moving the mouse to move the mouse, and so on; some events are not generated by user actions but by the system automatically, If the database link is disconnected, the time arrives at a certain point, or the peripheral is not ready for use.

In Windows Forms, each Windows control has very many events that enable it to interact with the user, and the monitoring and management of events is the responsibility of the application class described above. A series of actions are usually triggered when an event is generated, such as clicking the Refresh button to use the program to retrieve data from the database, clicking the Close icon on the form to close the window or exit the program, and so on. The programmer is to control and interact with the program by writing the response method of the event (that is, the action that the event causes).

In. NET, the declaration of an event is declared by the event (event) keyword, and the event is one of the members of a class.

. NET, the response and processing of the event is handled by a new data type Delegate (representative), delegate (representative) is very similar to the C + + function pointer, but the difference is that delegate (represented) is safe and managed.

Declare the code for a control click event in three languages, respectively, as follows:

public event EventHandler Click; [C #]

Public Event Click as EventHandler [Visual Basic]

Public: __event eventhandler* Click; [C + +]

In the above code, EventHandler is a representative type that is in the. NET class library are declared as follows:

public delegate void EventHandler (Object Sender,eventargs e); [C #]
Public Delegate Sub EventHandler (ByVal sender as Object,byval e as EventArgs) [Visual Basic]
Public __gc __delegate void EventHandler (object* sender,eventargs* e); [C + +]

Event Response Method

Since events are interpreted and managed by the Application object, programmers do not need to know when the user responds to an event or respond to an event, just add a response method to the event. By using the delegate (represented), you can use the "+ =" (plus equals) operator very easily for. NET class adds one or more response methods, and you can cancel these responses by using a very simple "=" (minus equals) operator. As below, we add the code for the response method for the Click event of a button:

Button1. Click + + new System.EventHandler (MyMethod);

Where the MyMethod function is defined as follows:

void MyMethod (object sender, System.EventArgs e);

This way the code in the MyMethod function executes when the user clicks the button or other method raises its Click event.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.