We know that in object-oriented programming, a class has many Members, including domains (Fields), constructors, destructors, methods (Methods), events, properties, and so on. Some of these members represent a feature of an object, and some use an object to perform an operation.
. NET class, which is primarily used to represent the characteristics of an object, such as the size of a form, its position, whether it can be maximized, whether the control bar and the background color of the form are displayed, and so on. Properties are public to programmers, and you can access properties by using a simple "=" (Assignment number). In a. NET class, some attributes are read-only, some can only write, and of course most properties are read-write, depending on the definition of the attribute in the class. As the following code defines a property in the form (form) class that shows whether a control bar is displayed:
public bool ControlBox {get; set;} [C #]
Public property ControlBox as Boolean[visual Basic]
With the above definition, to make a form do not display the control bar, use C # only to use the following simple statement:
Form1. Controlbox=false;
In Windows Forms programming, we all create a very beautiful form by setting a large number of attribute values. In addition, there are attributes that increase the language's support for RAD (rapid development).
Control:
In Windows forms programming, only forms are often not enough, and there must be a lot of control to enrich the contents of the form, to achieve a friendly, user-friendly interface design of human-machine operation. This,. NET class library prepares us for almost every kind of element class that will be used in Windows Forms, and in general we refer to these classes or their instances as controls, and the form is enriched by a variety of controls.
In fact, the concept of control is used in RAD (rapid development) tools, these development tools put a lot of commonly used classes and related content very visually placed in toolbars or panels, you only need to use simple mouse drag and drop to add a variety of controls to the program and set the various properties of the control, The appropriate code is generated automatically by the development tool. By using controls, you can make your entire development easier.
In the. NET class library, basically all of the controls are inherited by the System.Windows.Forms.Control class, which provides very basic, enables the class to visualize the display, handles various keyboard and mouse input, and responds to various events.
Because most controls are inherited from the control class, there is a property in the class that controls represents all the child control collections for the current control. Therefore, writing code to add a control to a form is simple enough to add the control object you are adding to the Controls property collection of the current form, such as the following C # code:
Form1. Controls.Add (New Button ());
A button control is added to the form Form1.
In addition, you can define your own controls, mainly through the System.Windows.Forms.UserControl class (which is a derived class of control), and of course, it is the same as using the ControlSource.
3 simple steps for Windows forms programming
With the previous introduction, you probably already know a design process for Windows Forms. In general, the steps for designing a Windows Forms program are as follows:
The first step: the analysis of procedural requirements
As a mature programmer, will be more detailed analysis of the needs of the program, and constantly communicate with the relevant personnel, and finally a more feasible solution. The requirement analysis is essential in the program design, so it is not too much to tell.
Step Two: Design your classes and objects
The most critical part of object-oriented programming is the object of the program processing problem. Therefore, you must spend more time on the object and class design of the problem that the program solves before writing the code. and solve the various transaction logic and process of the problem.
In general, object-oriented design is divided into two stages, namely high-rise design and low-level design.
The structure of the system is developed in the high level design stage, and the overall model of the software is constructed. At this stage, identify the concepts needed to solve problems in a specific computer environment, adding a batch of required classes that include classes that enable software systems to interact with the outside world. and output the appropriate application software requirements of the class, the relationship between classes and application subsystem of the view specification.
The low-level design focuses on the detailed design phase of the class. The goal of class design is to form a model of a single concept, an Independent class represents a concept, and the designed parts should be reusable and reliable.
The design principles of information concealment, cohesion well-structured classes coupling and so on should be adopted in the design process of class. In object-oriented technology, the reuse of the stored class is a great advantage.
In the class design process of Windows Forms, it is important to make full use of the various. NET already exists the reuse of the class, and on the basis of it increases the parts and functions required by the class.
Step Three: Interface design (design forms and controls)
Because the Windows Forms program focuses on the beauty and friendliness of the Man-machine interface. Therefore, in addition to writing a very high efficiency code and algorithm implementation program functions, Windows forms will also be the design of the interface as a focus.
Interface design is mainly by setting the control and form of various positions, size, shape, color and other appearance properties to achieve. Of course, to achieve a very user-friendly user interface, it is very difficult to complete the input of code through the software of Notepad and adjust the size, position and appearance of various controls according to the feeling, etc. (only you can write a larger program through Notepad), we must use a variety of development tools, The use of special rapid development (RAD) tools.
Currently, there are many that can be written. NET program, the RAD tool software, which is the most perfect, the most powerful, non-Microsoft visual Studio. Net Mo genus. The software is a rapid development (RAD) tool integrating multiple languages, which provides powerful software engineering management, computer resource management, Intelligent Code sensing technology and powerful program test and adjustment functions. Use Visual Studio. NET to develop your various. NET applications, everything will be much more effective, and you will find that programming becomes so easy.
By using visual Studio. Net, it's easy to design your form and program interface. You only need a single mouse drag and drop, position adjustment and so on to complete the program of most of the design. Visual Studio. NET automatically generates code templates and normalized annotations, and you can add various event response functions to them through a very simple double-click control. It is also supported by using Visual Studio. Net. NET, and other functions such as cross inheritance of multiple languages.
Step Fourth: Add various event response methods for forms and controls
After the form and interface design is complete, the rest is to add specific source code to the program. Since WindowsForms basically interacts with the user through a process such as raising an event to a response event to an execution event, most of the source code we write is a variety of event response methods for a control or form, along with some common functions, and so on. Combining effectively can make your program work according to your requirements.
If you use Visual Studio. Net, adding events is very simple, and most of the time you just double-click the control to add a common response method for the control. Other event-response methods can also be quickly added to the program with the help of their intelligent code-aware technology.
Step Fifth: Start the program
After everything is ready, you can now write the code for the startup program. Of course, if you use Visual Studio. Net, you might have code for starting a program that's ready for you. As described earlier, starting a Windows Forms program requires only the application class, which is a encapsulated class, and most of the methods are declared and cannot be used to create an instance of the class. Using application typically requires a form (not absolutely necessary), and the comprehensive design of the form class is now available and directly constructs an instance of your form class. The application class will do a lot for you, including memory and resource allocation, calls to various low-level components, and so on.
41 Complete and simple Windows Forms programs
Below we will list a complete and very simple Windows Forms program written in C # that defines one of its own form class HelloWorld through inheritance. The startup program will see a button control on the program, and clicking the button control will display a pop-up menu.
The program covers almost all of the previous points such as forms, controls, control properties, events, event responses, and so on. The code includes specific annotation instructions, please understand carefully!
HelloWorld.cs source Program
The using system;//introduces the System namespace, which includes the. NET most of the basic data types, such as int,string, etc.
Using system.drawing;//introduces the System.Drawing namespace, which includes a variety of basic image manipulation elements in Windows Forms
The using system.windows.forms;//introduces the System.Windows.Forms namespace, which defines most of the classes involved in the Windows forms program, such as forms, controls, application classes, and so on
The public class HelloWorld:System.Windows.Forms.Form//Defines a HelloWorld form class that has all the members and attributes of the form class by inheriting form
{
Private System.Windows.Forms.Button button1;//defines a member of the HelloWorld class (a button)
Constructor for public HelloWorld ()//helloworld class
{
The Button1=new button ()//generates a button instance, or it can be said to initialize the Button1 member of the HelloWorld class
Button1. text= "Testbutton";//Set the Text property value of the button button1
Button1. Location=new Point (100,100);//Set the Location value of the button button1
Button1. Click+=new System.EventHandler (MyMethod);//Add a response method to the Click event for the button button1
This. text= "Hello world!"; /sets the Text property of the custom form, which becomes the title of the form
This. Controls.Add (button1);//Add the button form to the form so that the form can be displayed.
}
public static void Main ()//Program entry
{
Application.Run (New HelloWorld ());//Generate HelloWorld instance and start the program through Application.Run () static method.
}
The private void MyMethod (Object Sender,system.eventargs E)//Defines the event response function, please note that the parameter is used in accordance with the parameters in the System.EventHandler representative
{
MessageBox.Show ("You have clicked the button", "Information");//Use pop-up popup to prompt the message.
}
}
To compile the program, copy the code directly into Notepad, save it as a HelloWorld.cs file, and then enter the CSC HelloWorld.cs at the command line to compile (assuming HelloWorld.cs is on the current directory). When the compilation is complete, a file named HelloWorld.exe is generated, and when you enter the HelloWorld program directly at the command line, you can run the program.
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.