C # development with winform Interface

Source: Internet
Author: User

Using vs2010 for development in windows is not studied in depth.

 

C # development with. net, a bunch of new terms, dizzy, such as CLR/apartments/STA/MTA/COM

Microsoft is really a software company, not a literature company?

1. Engineering code structure

After creating the Windows Forms Application project, the following code is automatically generated:

Form1.cs and Form1.Designer. cs are two files that define the behavior/style of a form. It will be folded together in vs2010.

The style is defined in Designer. Event listening and event processing are all defined in form. cs.

Double-click form. cs to open the UI. You can drag and drop the UI to complete the UI layout. Right-click the view code to view the form1.cs source code.

 

The main function is defined in Program. cs, which is the Startup File.

Use an App to run a form.

[STAThread]/[MTAThead] specifies the single/multi-thread running mode.

using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace AppDemo{    static class Program    {        /// <summary>        /// The main entry point for the application.        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new Form1());        }    }}
2. Form. cs and Form. Designer. cs

The form1.cs and designer. cs files define the same class in the same namespace.

When each file is defined, partial class is used.

Code of the interface layout, which is generally automatically generated in Designer.

Handwritten code is mainly used for event processing, which is generally stored in form. cs.

 

In the form. cs constructor, The InitializeComponent () defined in Designer. cs is called to complete interface initialization.

In InitializeComponent, the index private System. Windows. Forms. Button button1 of each control is declared;

In form. cs, you can directly use the variable name button1 to operate the control.

Therefore, in the form. cs constructor, InitializeComponent is generally called first.

In Designer. cs, the InitializeComponent initialization interface. Use the Dispose method to release resources. Do not cause resource leakage during release.

// --------------- Form1.cs -----------------------------------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 AppDemo{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }    }}// ------------------- Form1.Designer.cs -------------------------namespace AppDemo{    partial class Form1    {        /// <summary>        /// Required designer variable.        /// </summary>        private System.ComponentModel.IContainer components = null;        /// <summary>        /// Clean up any resources being used.        /// </summary>        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region Windows Form Designer generated code    }}
3. Resource files and resx

Exe or dll files must be dependent on other files for normal operation,

The simplest way is to copy the dependent file to the client, and read the file from the relative path in the program.

This is normal, but if the customer deletes these resources, it will lead to unexpected results.

These files can be embedded into exe or dll through resource files.

 

Resource files can be divided into two types:. resx and. resources. The former is in xml format, and the latter is in binary format.

The. resx file works normally only when the current solution has a. cs file with the same name. Resources does not have this restriction.

 

You can use ResourceWriter in System. Resources to Generate a resource file, Generate a file, and Close the file.

After creating an instance, you can use the AddResource method to add resources. The first parameter is the identifier. You can use the resource through the identifier in the code.

Resource files generally store three types of data: byte stream (byte []), object, and string ).

ResourceWriter rw = new ResourceWriter ("filename. resources "); rw. generate (); // Generate the rw file. close (); // Add Resource public void AddResource (str_identifier, byte []); public void AddResource (str_identifier, object); public void AddResource (str_identifier, str_value );
Resources. ApplyResources (this. myButton, str_identifier); // this. myButton uses the resource str_identifier
This. ApplyResource ();
4. Added support for multiple languages and localization 4.1

After adding a component on the interface, the. resx file is generated, and vs2010 is folded under the corresponding form. cs.

This is the default language resource file named form1.resx.

To develop other language versions, set Localizable to True in the attribute menu on the right of the corresponding form and set language to the required language.

Save the new display text and generate the. resx file in the corresponding language. The file name is in the format of form1.language-Location. resx. For example, form1.zh-CN. resx in simplified Chinese.

4.2 obtain and set the runtime Language

Two key concepts:

  • CurrentCulture: The default value is the user region setting of the operating system, which is set in the "region options" control panel.
  • CurrentUICulture: The default value is the UI Language of the operating system.
System. globalization. cultureInfo. installedUICulture. name; // obtain the current running language System. threading. thread. currentThread. currentUICulture = new System. globalization. cultureInfo ("zh-CHS"); // sets the current running language
5. Page Layout and style settings

After SuspendLayout () is called, the layout logic of the control is suspended until the ResumeLayout () method is called.

When you adjust multiple attributes of a control, the SuspendLayout method is called first, and the Size, Location, Anchor, or Dock attributes of the control are set,

Finally, call the ResumeLayout method to make the change take effect.

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.