Java Attack c#--application development WinForm Environment

Source: Internet
Author: User
Tags java jframe

This chapter is a brief statement

In the previous chapter, I talked about the IO file operation class to understand how to handle file streams. Starting from this chapter, I will explain the relatively advanced knowledge points. And the author of this chapter on the WinForm development of knowledge points to explain and guide. Now many businesses are oriented to the development of B/s mode, Java is not another. So Java programmers are very familiar with the knowledge of the EE section. But the knowledge of the J2SE half know half solution. Even some of them just listened to. I also used AWT and swing to develop PC-side applications before entering Java Enterprise development. Similar to applications such as inventory management and enterprise communications. People who believe they do know that a small business can take a lot of time to develop. Understanding C # WinForm Development you will find the PC-side software or C # more appropriate. In addition, starting from this chapter, I hope that Java readers can start switching learning ideas. Don't learn from the knowledge of Java. Because I've had C # write Java code.

First application of WinForm

Let's create a new WinForm project. Then come and learn how WinForm is developed. Select the file New project for Visual Studio development tools.

The picture above believes that everyone is not unfamiliar with it. Click OK to press the button. Build the project as follows

The author doesn't say much about the project structure. Much has been explained in the Java Attack c#--project development Environment section. Readers who are not clear can go and see. The two here are not explained. As follows

Form1.cs file: This is a form. The Java JFrame don't know that you understand. It's a bit similar.

Program.cs file: The application's entry method is stored here.

First, let's take a look at the code inside the Program.cs file. Let's take a look at what the C # Portal does.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Windows.Forms;5 6 namespaceWinformexample7 {8     Static class Program9     {Ten         /// <summary> One         ///The main entry point for the application.  A         /// </summary> - [STAThread] -         Static voidMain () the         { - application.enablevisualstyles (); -Application.setcompatibletextrenderingdefault (false); -Application.Run (NewForm1 ()); +         } -     } +}

The code above is automatically generated. Can you change it? Of course you can. The Application.Run method above is easy to read. Instantiates a Form object new Form1 (). The Run method is running the form. So what about the EnableVisualStyles method and the Setcompatibletextrenderingdefault method above? The author can only press F12 to look inside.

     //        // Summary:          //      enables the application's visual style. public        staticvoid enablevisualstyles ();
//        //Summary://set the UseCompatibleTextRendering property defined on some controls to the application-scoped default value. //        //Parameters://DefaultValue://the default value for the new control. If true, new controls that support usecompatibletextrendering use GDI +-based System.Drawing.Graphics//class for text rendering, or False if the new control uses the GDI-based System.Windows.Forms.TextRenderer class. //        //Exception://System.InvalidOperationException://You can call this method only before a Windows Forms application creates the first window.          Public Static voidSetcompatibletextrenderingdefault (BOOLDefaultValue);

See the official note. I still do not understand the point. So the author put the first method (EnableVisualStyles) to comment out. See what's going to happen. The result runs through. But it looks like a little weird. As follows

Before you comment out

After the comment is dropped

See it. The button shows a slightly different effect. Maybe this is the visual style. Of course, there's a way of saying this on the Web: The EnableVisualStyles method means that if the control and operating system support visual styles, the control will be drawn with visual styles. So how about the Setcompatibletextrenderingdefault method. Read the official instructions and know what the difference is between the graphics class and the TextRenderer class. Both of them control the text rendering on the control. I think the readers should find out for themselves. Relevant information is also available on the network. If you don't understand, there's nothing. Because it doesn't affect the development of your business.

Said so much but did not talk about how to increase the twist on the form inside. This is where Visual Studio is powerful. Once you get into WinForm development you have to understand a view-the toolbox. The general case kit is on the left side of Visual Studio. But here's one thing to note. If you select the Toolbox and you don't have a form selected. So there's nothing in the toolbox. So, double-click the Form1.cs file. This time the left side will automatically switch to the toolbox. Under

At this point, you can drag out the controls you want from the toolbox. Nothing wrong. Just drag it out and put it in the corresponding form position. is the visual programming of Dreamweaver software design.

All right. Drag a button onto the Form1.cs form. Don't say anything. Double-click the button to do it. The following is a double-click Button1. This is when you find yourself running to the corresponding code interface. The following code section.

C#:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacewinformexample{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {        }    }}

See the code the author will understand. The visual form is equivalent to telling Visual Studio what kind of form interface we want. The corresponding code is then generated by Visual Studio in the background. What do you mean? Let's look at the Code section first. There is a keyword partial. Meaning is a partial class. Some classes mean that sometimes the code inside the class is too much and too miscellaneous. It seems to be quite annoying. Then the classification can be divided into different files, but the class name of the same class. Of course, after compiling, they are automatically assembled into a class. No matter what it looks like. We see that Form1 inherits form forms. The equivalent of many forms in Java is inherited JFrame is a property. I believe I don't have to say more. Do you remember just double-clicking button1? In fact, I'm telling Visual Studio I'm going to give Button1 a click event to set the button. So private void Button1_Click (object sender, EventArgs e) is the Click event that corresponds to the Button1 twist.

Okay, first stop, let's finish the application. We are going to talk about the relevant knowledge points. Let's write a popup Hello World feature in this event.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 Ten namespaceWinformexample One { A      Public Partial classForm1:form -     { -          PublicForm1 () the         { - InitializeComponent (); -         } -  +         Private voidButton1_Click (Objectsender, EventArgs e) -         { +MessageBox.Show ("Hello World"); A         } at     } -}

Run click-to-tap. The results are as follows.

WinForm Environment

Remember that when you double-click button1 you can go to the back code interface. Of course you can also choose the corresponding form. Right-click to view the code. In the same way you can enter the background code interface. I think we should look at the Form.cs file before I understand the code interface. is shown below

After we see the Form1.cs file point open, three files appear below.

Form1.Designer.cs: Also the later Code section. Just this part of the Form1 class is part of the class file. The main is to put the code about the form assembly.

Form1.resx: is a file for internationalization settings. If you want to use his words, please be careful to modify the access modifier. Just like Java's properties file.

Form1: It is also the background code. The main is to put the user operation control or the code of the form. That's where developers are developed.

All right. This is the time you can not double-click butto1 into the background code. You can also view the code without right-clicking. Simply double-click on the corresponding file to do it.

WinForm Learning

One of the seniors said to learn WinForm, please learn the controls first. At that time there was no knowing why. Three years later only to understand, there is a certain truth. In the face of the current enterprise development many times do not need you to winform how much understanding. Even his message notification mechanism doesn't need to be understood. So how to learn to control it? In the above example, we can't always let the button be called Button1. Always have to change some values. This is the time to use a called attribute form. Select the button right-click property of the example above. The corresponding form will pop up at this time. Under

What is a property form? When we develop a Java form, we always have to set the position of the button, the color of the button, the name of the button, and so on. Nothing wrong. Property forms are the property values that are used to set these controls. So learning these attributes is a must. When you select the attribute section, the following is the required form to display his description. This is how the author learns. Let the author raise a column bar.

See above no author modifies two places: the Text property and the Name property. Let's take a look at the backstage code. Select the Form1.Designer.cs file to double-click. Why is he? Remember above that he is assembling the form code to store the place.

namespacewinformexample{Partial classForm1 {/// <summary>        ///the required designer variables. /// </summary>        PrivateSystem.ComponentModel.IContainer components =NULL; /// <summary>        ///clean up all the resources that are in use. /// </summary>        /// <param name= "disposing" >true if the managed resource should be disposed, otherwise false. </param>        protected Override voidDispose (BOOLdisposing) {            if(Disposing && (Components! =NULL) ) {components.            Dispose (); }            Base.        Dispose (disposing); }        #regionCode generated by the Windows Forms Designer/// <summary>        ///The Designer supports the required method-do not///Use the Code Editor to modify the contents of this method. /// </summary>        Private voidInitializeComponent () { This. Btnfirst =NewSystem.Windows.Forms.Button ();  This.            SuspendLayout (); //             //Btnfirst//              This. btnfirst.location =NewSystem.Drawing.Point (103, the); this.btnFirst.Name = "Btnfirst";  This. btnfirst.size =NewSystem.Drawing.Size ( the, at);  This. Btnfirst.tabindex =0; This.btnFirst.Text = "First Application";  This. Btnfirst.usevisualstylebackcolor =true;  This. Btnfirst.click + =NewSystem.EventHandler ( This. Button1_Click); //             //Form1//              This. Autoscaledimensions =NewSystem.Drawing.SizeF (6F, 12F);  This. AutoScaleMode =System.Windows.Forms.AutoScaleMode.Font;  This. ClientSize =NewSystem.Drawing.Size (284,261);  This. Controls.Add ( This. Btnfirst);  This. Name ="Form1";  This. Text ="Form1";  This. ResumeLayout (false); }        #endregion        Private System.Windows.Forms.Button Btnfirst; }}

The red code above is the code that corresponds to the change.

The function of the attribute form I believe we all understand. There are four buttons on the menu bar of the Properties form. Their role is, respectively, as below.

: Use to arrange properties by category.

: Used to arrange attributes by letter.

: Used to display properties.

: Used to display events.

The preceding list is all set to properties. So is it a bit of an event where the button is set? At this point the fourth button will come out. Click on him. You can set the corresponding event. You can also learn the corresponding events.

Here we all know how to set the properties and things for each control. The next step is to understand the role of a control. Then the role of the toolkit comes out. As follows.

See the article for the Toolbox the author can understand that the controls are also categorized. There are for layouts. There is a display for. have to interact with. have access to data. If the container is for layout. Believe that the reader should know what to understand.

This chapter summarizes

In this chapter, I explain how to learn WinForm. At the same time familiar with WinForm development environment.

Java Attack c#--application development WinForm Environment

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.