Java attack C # -- WinForm development for application development,
In the previous chapter, I introduced the WinForm environment. In this chapter, I will continue to talk about WinForm. But it is more development-oriented. The fact is the control in the Learning toolbox. For WinForm development, enterprises do not have such high requirements. But if it's game-related, sorry! I think you may have chosen the wrong language. C ++ may be more suitable for you. I hope readers will understand this. The following content is used by the author during development. It can only help you learn, but cannot be a teaching material. In the previous chapter, WinForm development is JAVA's Awt and Swing programming. I have been doing Awt and Swing for more than four years. If you remember nothing wrong, I often use layout classes such as FlowLayout, BorderLayout, and JPanel when designing a form. So let's take a look at the WinForm layout first.
When it comes to layout, I can only start with the attribute form. I remember that most of the controls are inherited from the Control class. The Control class has two attributes related to the layout. One is the Anchor attribute and the other is the Dock attribute.
The Anchor attribute binds the control to the edge of its container and determines that the control is adjusted along with its container. However, I am not very clear about how to adjust the ratio. No such statement was found in MSDN. It only indicates that the control is adjusted along with its container. The value of the Anchor attribute is as follows.
Public enum AnchorStyles {// Abstract: // The control is not anchored to any edge of its container. None = 0, /// Summary: // The control is anchored to the upper 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, when you pull the container to the Right, the size of the control is also changed to the Right. Let me give you an example.
From the figure, we can see that I have set four values-Up, down, and left. Now I will drag them to see their changes.
The button is placed in a Panel container. Now the author dragged the Panel to find the changes along with the button.
The Dock attribute is similar to the BorderLayout class in JAVA. The containers are divided into the top, bottom, left, right, and middle areas. Therefore, there are also five values for the corresponding Dock attribute.
1 public enum DockStyle 2 {3 // Abstract: 4 // The control is not docked. 5 None = 0, 6 // 7 // Summary: 8 // the upper edge of the control stops at the top of the control. 9 Top = // 11 // Summary: 12 // the lower edge of the control is docked at the bottom of the control it contains. 13 Bottom = // 15 // Summary: 16 // the left edge of the control is docked on the left edge of the control. 17 Left = 3, 18 // 19 // Summary: 20 // the right edge of the control is docked at the right edge of the control. 21 Right = 4,22 // 23 // Abstract: 24 // each edge of the control is docked at each edge of the control, and the size is adjusted as appropriate. 25 Fill = 5, 26}
You can try it on your own. Is to set the corresponding value. It is the zone that can be docked at zero distance. Forget it. Set TOP. Let's take a look.
The features of the above two attributes are much better understood. Next, let's talk about the FlowLayoutPanel control. JAVA's FlowLayout is somewhat similar. Flow layout. C # is called the streaming layout panel. Is a container. Let's look at the picture below.
We can see that if there is not enough place to place a button, it will automatically store the line feed. The FlowLayoutPanel control contains a FlowDirection attribute to set the flow direction. Is from left to right, is from top to bottom. Readers can check this information on their own.
During the development process, we will find that the official controls sometimes cannot meet our own business needs. What should we do at this time? In fact, developers can assemble a control by themselves. It is a user-defined control. Right-click the project and choose add user control.
Enter the corresponding user control name and click Add button. Visual studio switches to the design form interface of the user control, and an additional file is displayed in the project. As follows:
The above project structure picture. We can see an extra file named ATextBox. cs. The icons in front of the file are different from those in the form. However, you will find that the operation is the same as the form operation. No error. You can operate the interface as a form. I believe I don't have to say much. Drag the control.
If you are careful, you can open it. At this time, you will find that there is something really like?
Drag a TextBox to ATextBox. cs. Then, drag and change the size of ATextBox. cs. .
Okay. That's it. So what is the purpose? Double-click Form1.cs form. At this time, we will find some minor changes in the corresponding toolbox.
See here. I don't need to mention pulling the corresponding form to use it. NOTE: If your user control has been modified in code. It must be regenerated. In this case, it may be the latest. In order to distinguish the background color of ATextBox. cs from red. Let's take a look.
Let's switch to the code below. The author adds an attribute named 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 10 namespace WinFormExample11 {12 public partial class ATextBox : UserControl13 {14 public ATextBox()15 {16 InitializeComponent();17 }18 public string AText19 {20 set { this.textBox1.Text = value; }21 get { return this.textBox1.Text; }22 }23 }24 }
Let's take a look at this time. Corresponding properties will appear in the corresponding property form. In this way, you can modify the value.
I believe that the readers can understand what the author wants to express. No error. If you write related events. It will also appear in its property form. So what if I don't want him to appear in the attribute form? At this time, we need to look at a class. The UserControl class is the best learning. Follow F12 to view the UserControl class. We will find some annotations. Such as Browsable and EditorBrowsable. No error. These are used to indicate the behavior of the corresponding attribute in the attribute form. I don't have to say much.
[Browsable(true)][DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)][EditorBrowsable(EditorBrowsableState.Always)]public override bool AutoSize { get; set; }
I will write about the user control here. This part of content is rarely used in the development process. However, for software development on a large PC end. This is generally used. Read this information by readers.
| Application configuration file |
The application configuration file is App. config. This is. NET's own configuration file with the. config extension (in fact, it is an XML file ). Therefore, the XmlDocument class is not required for reading. Of course, you can use XmlDocument if you feel uncomfortable. This does not have much impact. However, for the sake of professionalism, sometimes I still use the C # reading method. What is App. config? It is generally used in WinForm development. You can place the corresponding database connection string in App. config. You can also store other information. No error. Is used to store application configuration information. Then C # mentions a class called ConfigurationManager. This class is used to read App. config. Let me give you an example.
The ConfigurationManager class is in the dll of System. Configuration. If the project reference is useless. Sorry. Please introduce it first. I believe that "Java attack C # -- Project Development Environment" is enough for you to understand. Then, you can create an App. config. Right-click the project and choose add> new item. I believe everyone understands this.
In general, you can find the corresponding "application configuration file. Select "add ". The content of App. config. appears in the project as follows.
<?xml version="1.0" encoding="utf-8" ?><configuration></configuration>
Okay. All we need to do is add relevant information in App. config.
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="Aomi" value="i am aomi"/> </appSettings></configuration>
At this time, I will read the relevant information. I dragged a Label control and a button control in the form. Click the button to read the configuration information value. Assign a value to the Label control.
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 11 namespace WinFormExample12 {13 public partial class Form1 : Form14 {15 public Form1()16 {17 InitializeComponent();18 }19 20 private void button1_Click(object sender, EventArgs e)21 {22 this.label1.Text = ConfigurationManager.AppSettings["Aomi"];23 }24 }25 }
See the code. Just a simple sentence. Let's take a look at the running status.
The App. config is not just described by the author. It can also be used to set system settings for software development. This is much more. It cannot be completed. For more information, see.
This class can be understood as a background thread. In the development process, the program has a lot of work that users do not need to see. These jobs can generally be handed over to the following threads for work. The BackgroundWorker class is the best representative. For the BackgroundWorker class, I should have discussed it in the multi-thread section. However, I think it is more appropriate to put it here. The WinForm interface is executed in a UI thread. A special feature of the UI thread is that the background thread cannot access the controls in the UI thread. Generally, an Invoke method is used for access. It is in the Control class. Therefore, this method is available for both controls and forms. Let's look at the code below.
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 12 namespace WinFormExample13 {14 public partial class Form1: Form15 {16 public Form1 () 17 {18 InitializeComponent (); 19} 20 21 private void button#click (object sender, eventArgs e) 22 {23 this. label1.Text = ConfigurationManager. appSettings ["Aomi"]; 24} 25 26 private void Form1_Load (object sender, EventArgs e) 27 {28 Thread myThread = new Thread (this. trace); 29 myThread. start (); 30} 31 32 public void Trace () 33 {34 this. invoke (new Action () => 35 {36 this. label1.Text = "this is a Trace"; 37}); 38} 39} 40}
If you remove it, an exception occurs. As follows:
Let me give a column to illustrate the usage of the 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 12 namespace WinFormExample13 {14 public partial class Form1 : Form15 {16 private BackgroundWorker _backgroudWorker;17 public Form1()18 {19 InitializeComponent();20 }21 22 private void button1_Click(object sender, EventArgs e)23 {24 if (this._backgroudWorker == null)25 {26 this._backgroudWorker = new BackgroundWorker();27 this._backgroudWorker.WorkerSupportsCancellation = true;28 this._backgroudWorker.WorkerReportsProgress = true;29 this._backgroudWorker.DoWork += BackgroudWorker_DoWork;30 this._backgroudWorker.ProgressChanged += BackgroudWorker_ProgressChanged;31 this._backgroudWorker.RunWorkerCompleted += BackgroudWorker_RunWorkerCompleted;32 }33 34 this._backgroudWorker.RunWorkerAsync();35 36 }37 38 private void BackgroudWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)39 {40 this.progressBar1.Value = e.ProgressPercentage;41 }42 43 private void BackgroudWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)44 {45 this.label1.Text = e.Result.ToString();46 }47 48 private void BackgroudWorker_DoWork(object sender, DoWorkEventArgs e)49 {50 string value = ConfigurationManager.AppSettings["Aomi"];51 e.Result = value;52 53 for (int i = 0; i < 100; i++)54 {55 this._backgroudWorker.ReportProgress(i);56 Thread.Sleep(200);57 }58 59 }60 61 }62 }
Running result
The BackgroundWorker class separates the backend from the foreground. In addition, you can notify the front-end during execution in the background. We can see that the BackgroundWorker class is still very powerful. You can also skip the Invoke method.
Workersuppscanscancellation attribute: indicates whether the job can be canceled during running.
WorkerReportsProgress attribute: Used to indicate whether the frontend can be notified during running.
DoWork event: Background thread
ProgressChanged event: foreground UI thread. It is used for notification execution in background threads.
RunWorkerCompleted event: foreground UI thread. Used for execution after the background thread ends.
RunWorkerAsync method: Start asynchronous operation.
This chapter focuses on several knowledge points used by the author in the development process. I also think that WinForm learning must be understood. Here is a brief introduction to WinForm. In the next chapter, let's take a look at the knowledge points about Linq and EF.