Java Attack c#--application development WinForm Development

Source: Internet
Author: User

This chapter is a brief statement

In the previous chapter, I introduced the WinForm environment. The author of this chapter will continue to speak WinForm. It's just more development-oriented. The fact is that you are learning the controls inside the toolbox. For WinForm Development, the demand for his business is not so high. But if it is game-related, sorry! I think you may have chosen the wrong language. C + + may be more appropriate for you. A little hope that the readers will understand. The following is a description of the author's development. Can only help you to learn, but can not become teaching materials. The previous chapter on WinForm development is Java's AWT and swing programming. I have been doing AWT and swing for over four years. If you remember not wrong, in the design of the form, I often use about the FlowLayout class, BorderLayout class, JPanel class and other layout classes. So let's take a look at the layout of WinForm first.

WinForm Layout

When it comes to layout, I can only cut into the attribute form first. Remember that most of the controls are inherited from the control class. There are two attributes in the control class that are related to the layout. One is the Anchor property and one is the dock property.

The purpose of the Anchor property is to bind the control to the edge of its container and to determine that the control is resized with its container. However, the author is not very clear as to what kind of comparison column to adjust. MSDN also did not find the relevant statement. It simply means that the control is resized with its container. The following is the value of the Anchor property.

    Public enum AnchorStyles    {        //Summary:         //     The control is not anchored to any edge of its container.        None = 0,        /////        Summary:         //     the control is anchored to the top edge of its container.        Top = 1,        ////        Summary:         //     the control is anchored to the bottom edge of its container.        Bottom = 2,        ////        Summary:         //     the control is anchored to the left edge of its container. Left        = 4,        ////        Summary:         //     the control is anchored to the right edge of its container. Right        = 8,    }

If you set right, the size of the control will change to the right when you pull the container to the left. Let me give you an example.

From the figure can be seen that the author set four values-up and down around. Now I'll drag you to see how they change.

The button is placed inside a panel class container. Now I drag the panel is found to follow the twist followed by the discovery of changes.

Dock properties This can be said to be the Java BorderLayout class is a look. The container is divided into upper, lower, left, right and middle five regions. So there are also five values for the corresponding dock property.

1 public     enum DockStyle 2     {3         //Summary:  4         //     The control is not docked. 5         None = 0, 6         //7         //Summary:  8         //     the upper edge of the control is docked at the top of its containing control. 9         Top = 1,10         //11         //Abstract:/         /The     lower edge of the control is docked at the bottom of its containing control.         Bottom = 2,14         //15         //Abstract: +         //     the left edge of the control is docked at the left edge of its containing control.         3,18 =         //19//         Abstract://The     right edge of the control is docked at the right edge of its containing control.         right = 4,22         //23//         Summary:     each edge of the control is docked at each edge of its containing control, and is resized appropriately.         Fill = 5,26     }

You can try it yourself. is to set the corresponding value. Is the area that can be set by stopping at zero distance. Never mind. I set up the top bar. Look at it.

The functionality of the above two properties is probably more familiar. Let's take a look at the FlowLayoutPanel control. Java's flowlayout is a bit similar. It means a mobile layout. And this is called a streaming layout panel in C #. is a container. Take a look at the picture below.

We can see that if there is not enough place to put a button, it will be automatically wrapped. The FlowLayoutPanel control has a property called FlowDirection to set the direction of the flow. is from left to right, from top to bottom. This one? Readers check it out for themselves.

User-defined controls

In the development process, we will find that the official provided controls sometimes do not meet our own business needs. What about this time? In fact, developers can fully assemble a control themselves. That is, a user-defined control. Select the project right-click Add User Control.

After filling in the name of the corresponding user control, click Add button. Visual Studio switches to the user control's design form interface, and there is one more file in the project. As follows

Picture of the project structure above. We can see an extra file called ATextBox.cs. The icon in front of the file is not the same as the icon in the form. But you will find that the operation is the same as the form operation. Nothing wrong. You can take this interface as an action on the form. I believe I don't have to say more. Drag the drag control.

If you're careful, you can open him up. This time you will find that there is really a little like it?

The author drag a TextBox into the ATextBox.cs inside it. Then drag to change the size of the ATextBox.cs. Under

All right. That's it. So what's the use? Double-click the Form1.cs form. This time we will find some minor changes in the corresponding toolbox.

See here. Do not have to say that the author to pull to the corresponding form can be used. One thing to note here is that if your user control has code modifications. Must be regenerated. This is the time to pull it out and be up to date. In order to distinguish the author of the ATextBox.cs color changed to red. Let's take a look at it.

Let's cut the code to the back. The author adds a property called Atext in the code. As follows

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 namespace WinFormExample11 {public     partial class Atextbox:usercontrol13 {+ Public         Atextbox () 15
   {16             InitializeComponent ();         }18 public         string AText19         {             set {This.textBox1.Text = value;}             get {return this.textBox1.Text;}         }23     }24}

Let's take a look at this time. Corresponding attributes will appear in the corresponding attribute form. This way, you can change his value.

I believe the reader can understand what the author is meant to express. Nothing wrong. If you write a related event. will also appear in his properties form. So sometimes what if you don't want him to appear in the Properties form? This time we are going to see a class. The UserControl class is the best learning. Press F12 tearful UserControl class. We'll find some annotations. such as browsable, editorbrowsable and so on. Nothing wrong. These are used to represent the behavior of the corresponding property in the Properties form. I don't have to say more.

[Browsable (True)] [DesignerSerializationVisibility (Designerserializationvisibility.visible)] [Editorbrowsable (editorbrowsablestate.always)]public override bool AutoSize {get; set;}

About user controls This is where the author writes. This part of the content of the author in the development process is seldom used. But if it's a large PC-side software development. That would normally be used. Readers are asked to check them out.

Application configuration file

The application configuration file is app. config. This is. NET to create a configuration file (in fact, an XML) with a. config extension. So the XmlDocument class is not allowed on the read. Of course you can use XmlDocument if you feel uncomfortable. This does not have much impact. But for the professionalism of the author sometimes I will use C # reading. What is app. config? He is generally used in the development of WinForm. You can put the corresponding database connection string in app. Config. You can also store some other information. Nothing wrong. Is the configuration information that is used to store the application. Then C # itself to mention a called ConfigurationManager class. This class is used to read App. CONFIG. Not much to say let me cite examples.

The ConfigurationManager class is inside the dll of the system.configuration. If the project references are useless. Excuse me. Please give a primer first. In this regard, I believe that the Java Attack c#--project development environment is enough to let you know. Then, you can create an app. config. Select Item Right-click Add to create a new item. I believe we all understand it.

The corresponding "Application configuration file" can be found in the general. Select "Add". App. Config will appear within the project. The content is as follows.

<?xml version= "1.0" encoding= "Utf-8"?><configuration></configuration>

All right. All we have to do is add the relevant information in app. CONFIG.

<?xml version= "1.0" encoding= "Utf-8"?><configuration>  <appSettings>    <add key= "Aomi" Value= "I am Aomi"/>  </appSettings></configuration>

This time the author is going to read the relevant information. The author drags a label control and a button control in the form. Click on the button to read the value of the configuration information. Assigns a value to the label control.

Back code

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 using system.configuration;10 one namespace WinFormExample12 {public     partial class Form1:form14     {         Lic Form1 ()             InitializeComponent (),         }19         private void Button1_Click (object sender, EventArgs e)         {             This.label1.Text = configurationmanager.appsettings["Aomi"];23         }24     }25}

See the code. Just a very simple word to it. Let's take a look at the running situation.

About App. Config is not just the author's story. It can also be used to set up some system settings for the development software. That's a lot to do. There is no writing to be done. Please consult the authors themselves.

BackgroundWorker class

This class you can understand as a background thread. In the development process, the program has a lot of work that does not require the user to see. These jobs can usually be handed over to the back thread to work. And the BackgroundWorker class is the best representation. For the BackgroundWorker class originally the author should be in the multi-threaded section of the story. But I think it is more appropriate to put it here. WinForm's interface execution is done in a thread called the UI. One feature of the UI thread is that the background thread is not able to access the controls inside the UI thread. A general visit is called the Invoke method. He is inside the control class. So both the control and the form have this method. Take a look at the code below.

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using Syst Em. Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms;  9 using system.configuration;10 using System.threading;11 namespace WinFormExample13 {public partial class Form1  : Form15 {Form1 () + InitializeComponent ();}20 void Button1_Click (object sender, EventArgs e), {this.label1.Text = Configurationmanager.appsetti  ngs["Aomi"];24}25-private void Form1_Load (object sender, EventArgs e) MyThread = new Thread (this. Trace), Mythread.start (), and}31 (public void Trace ().         Invoke (New Action () =>35 {This.label1.Text = "This is a Trace"; 37}); 38 }39}40} 

If it is removed, an exception will occur. As follows

I would like to give a Lezilai explain the use of BackgroundWorker class.

 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 using system.configuration;10 using System.threading;11 namespace WinFormExample13 {public partial class Form1 : Form15 {Private BackgroundWorker _backgroudworker;17 public Form1 ()             Itializecomponent ();}21-private void Button1_Click (object sender, EventArgs e) 23 {24 if (This._backgroudworker = = null) This._backgroudworker = new BackgroundWorker (); 2 7 This._backgroudworker.workersupportscancellation = true;28 This._backgroudworker.workerr Eportsprogress = true;29 This._backgroudworker.dowork + = backgroudworker_dowork;30 this._b Ackgroudworker.progresschanged + = Backgroudworker_progresschanged;31 this._backgroudworker.runworkercompleted + = backgroudworker_runworkercompleted;32}33 34 This._backgroudworker.runworkerasync ();}37-private void backgroudworker_progr Esschanged (object sender, ProgressChangedEventArgs e), max. {this.progressBar1.Value = E.progresspercen tage;41}42 private void backgroudworker_runworkercompleted (object sender, Runworkercompletedeventargs e) This.label1.Text = E.result.tostring ();}47-private void Backgroudworker _dowork (object sender, DoWorkEventArgs e), {$ string value = configurationmanager.appsettings["Aomi" ];51 E.result = value;52 for (int i = 0; i <; i++) [Thi]     S._backgroudworker.reportprogress (i); Thread.Sleep (200); 57}58 59}60 61 }62}

Run results

The BackgroundWorker class separates the backstage from the front desk. You can also notify the foreground during background execution. We can see that the BackgroundWorker class is still very powerful. You can also use the Invoke method.

Workersupportscancellation property: Used to indicate whether work can be canceled in the run.

Workerreportsprogress property: Used to indicate whether the foreground can be notified in the run.

DoWork Event: Background thread

ProgressChanged event: Foreground UI thread. Used for notifications in the background thread when executing.

RunWorkerCompleted event: Foreground UI thread. Used to execute after the background thread has ended.

RunWorkerAsync method: Begins an asynchronous run.

This chapter summarizes

This chapter is mainly about the author in the development process to use several points of knowledge. Also I think that learning WinForm must understand. About WinForm The author of the simple introduction here. In the next chapter we're going to look at the points of knowledge about LINQ and EF.

Java Attack c#--application development WinForm Development

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.