Java attack C # -- WinForm environment for application development,

Source: Internet
Author: User
Tags java jframe

Java attack C # -- WinForm environment for application development,

Summary of this Chapter

In the previous chapter, I talked about IO file operations and how to process file streams. Starting from this chapter, I will explain more advanced knowledge points. In this chapter, I will explain and guide the knowledge points of WinForm development. Nowadays, many businesses are oriented to the development of B/S mode, and JAVA is no different. Therefore, JAVA programmers have a good understanding of J2EE. But has a half-knowledge and half-solution to J2SE knowledge points. Some of them are just heard. I have also used Awt and Swing to develop PC applications before entering JAVA Enterprise Development. Similar to applications such as inventory management and enterprise communication. I believe that people who have done this know that a small business takes a lot of time to develop. After learning about C # WinForm development, you will find that PC-side software is suitable for C. In addition, starting from this chapter, I hope that JAVA readers can begin to switch their learning ideas. Do not study with the knowledge of JAVA. Because I have written JAVA code in C.

First WinForm Application

Let's create a WinForm project. Then we will learn how WinForm is developed. Select "file" and "new" for Visual Studio development tools.

I believe everyone is familiar with the above picture. Click OK. The generated project is as follows:

I will not talk much about the project structure. I have explained most of the content in Java attack C #-project development environment. Readers who are not clear can go and have a look. I haven't explained either of them. As follows:

Form1.cs file: this is a form. JAVA JFrame is unknown to you. A little similar.

Program. cs file: contains the entry method of the application.

First, let's take a look at the code in the Program. cs file. Let's take a look at what the C # portal has done.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. windows. forms; 5 6 namespace WinFormExample 7 {8 static class Program 9 {10 /// <summary> 11 // main entry point of the application. 12 /// </summary> 13 [STAThread] 14 static void Main () 15 {16 Application. enableVisualStyles (); 17 Application. setCompatibleTextRenderingDefault (false); 18 Application. run (new Form1 (); 19} 20} 21}

The above code is automatically generated. Can it be changed? Of course. The preceding Application. Run method is easy to understand. Instantiate a form object new Form1 (). The Run method is to Run this form. What about the above EnableVisualStyles method and SetCompatibleTextRenderingDefault method? I can only follow F12.

//// Summary: // enable the visual style of the application. Public static void EnableVisualStyles ();
//// Summary: // set the UseCompatibleTextRendering attribute defined on some controls to the default value within the application range. //// Parameter: // defaultValue: // The default value for the new control. If it is true, the new control supporting UseCompatibleTextRendering uses the System based on GDI +. drawing. graphics // class for text rendering; if it is false, the new control uses the System based on GDI. windows. forms. textRenderer class. //// Exception: // System. InvalidOperationException: // you can only call this method before the Windows Forms Application creates the first window. Public static void SetCompatibleTextRenderingDefault (bool defaultValue );

See the official instructions. I still do not understand this. So I will annotate the first method (EnableVisualStyles. See what will happen. The result is successfully run. But it looks a little strange. As follows:

Before annotation Removal

After Annotation

See it. The displayed effect is slightly different. Maybe this is a visual style. Of course, there is a saying on the network: The EnableVisualStyles method indicates that if the control and the operating system support visual styles, the control will be drawn in visual styles. So what about the SetCompatibleTextRenderingDefault method. After reading the official instructions, you will know the differences between the Graphics class and the TextRenderer class. Both of them control text rendering on the control. Specifically, I think readers should find it by themselves. Relevant information is also available on the Internet. If you do not understand it, there is no problem. This does not affect your enterprise development.

After talking about this, I didn't talk about how to add a button in the form. This is the power of Visual Studio. Once you enter WinForm development, you must understand a view-toolbox. In general, the toolbox is on the left side of Visual Studio. But pay attention to it here. If you select the toolbox but do not select the form. There is nothing in the toolbox. Double-click the Form1.cs file. At this time, the left side will automatically switch to the toolbox. Lower

In this case, you can drag the desired control from the toolbox. No error. Drag it out and place it in the corresponding form location. It is just like the visual programming of dreamweaver software design.

Okay. Drag a button to the Form1.cs form. Do not double-click the button. Double-click button1. At this time, you will find that you have gone to the corresponding code interface. The following code is used.

C #:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WinFormExample{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {        }    }}

The author understands the code. A Visual form is equivalent to a form interface that tells Visual Studio what we want. Then the Visual Studio background generates the corresponding code. What do you mean? Let's take a look at the code. One keyword is partial. This indicates a partial classification. Partial classification means that sometimes the code in the class is too many and too complex. It seems quite annoying. In this case, codes can be differentiated into classes with the same class name but different files. Of course, they will be automatically assembled into a class after compilation. Whatever it looks. We can see that Form1 inherits the Form. It is equivalent that many forms in JAVA inherit JFrame. I don't need to say that. Do you still remember double-click button1? In fact, it tells Visual Studio that I want to configure a click event for button1. Therefore, private void button1_Click (object sender, EventArgs e) is the corresponding Click Event of button1.

Let's stop. Let's finish the application first. Let's talk about the relevant knowledge points. Let's write a pop-up Hello world function in this event.

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace WinFormExample11 {12     public partial class Form1 : Form13     {14         public Form1()15         {16             InitializeComponent();17         }18 19         private void button1_Click(object sender, EventArgs e)20         {21             MessageBox.Show("Hello world");22         }23     }24 }

Click to press. The result is as follows.

WinForm Environment

Remember to double-click button1 To Go To The Code interface. Of course, you can also select the corresponding form. Right-click to view the code. In the same way, you can enter the background code interface. Before learning about the code interface, I think we should take a look at the Form. cs file. As shown in

We can see that after the Form1.cs file is opened, three files will appear below.

Form1.Designer. cs: it is also part of the subsequent code. This part is only a partial classification file of the Form1 class. It mainly includes code about form assembly.

Form1.resx: indicates an international setting file. If you want to use it, you must modify the access modifier. This is just like the JAVA Properties file.

Form1: Background code. It is mainly used to place user operation controls or form code. This is where developers develop

Okay. In this case, you do not need to double-click butto1 to enter the background code. You do not need to right-click to view the code. Double-click the corresponding file.

WinForm Learning

One of the predecessors said that learning WinForm should be learned first. At that time, I did not know why. Three years later, I realized that there is still some truth. In the face of enterprise development, you do not need to know much about WinForm. Even his messaging mechanism does not need to be understood. So how to learn control? In the above example, we cannot keep calling the button button1. Always modify some values. In this case, an attribute form is used. Select the "right-click" attribute in the preceding example. The corresponding form is displayed. .

What will happen to the attribute form? When developing a JAVA form, we always need to set the location, color, and name of the button. No error. The property form is used to set the property values of these controls. Therefore, learning these attributes is required. When you select the attribute part, it is mandatory. The corresponding description is displayed below the form. I learned this way. Let me give you a column.

As shown above, I have not modified the Text and Name attributes. Let's take a look at the background code. Double-click the Form1.Designer. cs file. Why is it him? I remember that he stored the Assembly form code above.

Namespace WinFormExample {partial class Form1 {/// <summary> /// a required designer variable. /// </Summary> private System. ComponentModel. IContainer components = null; // <summary> /// clear all resources in use. /// </Summary> /// <param name = "disposing"> true if the managed resource should be released; otherwise, false. </Param> protected override void Dispose (bool disposing) {if (disposing & (components! = Null) {components. dispose ();} base. dispose (disposing );} # region Windows Form Designer generated code /// <summary> /// the designer supports the required methods-do not // use the code editor to modify the content of this method. /// </Summary> private void InitializeComponent () {this. btnFirst = new System. windows. forms. button (); this. suspendLayout (); // btnFirst // this. btnFirst. location = new System. drawing. point (103, 80); this. btnFirst. name = "btnFirst"; this. btnFirst. size = new System. drawing. size (75, 23); this. btnFirst. tabIndex = 0; this. btnFirst. text = "First Application"; this. btnFirst. useVisualStyleBackColor = true; this. btnFirst. click + = new System. eventHandler (this. button#click); // Form1 // this. autoScaleDimensions = new System. drawing. sizeF (6F, 12F); this. autoScaleMode = System. windows. forms. autoScaleMode. font; this. clientSize = new System. 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 changed code.

I believe everyone understands the role of the attribute form. There are four buttons on the menu bar of the property form. Their roles are as follows.

: Used to sort attributes by category.

: Used to arrange attributes alphabetically.

: Displays attributes.

: Used to display events.

The above columns are set attributes. So where can I set click events and other events? In this case, the fourth button is used. Click him. You can set the corresponding event. You can also learn the corresponding events.

Now everyone knows how to set the attributes and things of each control. The next step is to understand the role of a widget. Then the role of the Toolbox will come out. As follows.

The author can see the entries in the toolbox to understand that the controls are also classified. For layout. Used for display. It is used for interaction. Used to access data. For example, containers are used for layout. I believe that readers should know what to learn.

Summary

This chapter explains how to learn WinForm. Familiarize yourself with the WinForm development 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.