C # Advanced Programming (Sixth Edition) Learning: Chapter 31st: Windows Forms

Source: Internet
Author: User

31st Chapter Windows Forms

    1. Create a Windows Forms application

In the text editor, enter:

/*

* Form.cs

* A simple Windows Form

* */

Using System;

Using System.Windows.Forms;

?

Namespace Notepadforms

{

public class MyForm:System.Windows.Forms.Form

{

Public MyForm ()

{

}

?

[STAThread]

static void Main ()

{

Application.Run (New MyForm ());

}

}

}

Compile with the command Csc/target:winexe Form.cs, generate Form.exe to get a blank small form with no caption.

?

The main function is the default entry for the client application. In a large application, the main method is not in the form, but in the class, and is responsible for completing the required startup.

[STAThread], which sets the COM threading model to single-threaded apartment (single-threaded Apartment, STA). COM interaction requires the STA threading model, which is added to the Windows Forms project by default.

The Application.Run () method is responsible for starting the standard application message loop. It has three overloaded versions: the first version has no parameters, the second overloaded version takes the ApplicationContext object as its argument, and the third overloaded version takes the form object as a parameter.

?

Related functions and properties in the application class

methods/Properties

commonappdatapath

basepath for userdir\applicationdata

ExecutablePath

The path and file name of the executable file that launched the application

LocalUserAppDataPath

Similar to Commonappdatapath, supports roaming

Messageloop

Returns true if the current thread has a message loop, false otherwise

StartupPath

Similar to ExecutablePath, but does not contain a file name, contains only the path

Addmessagefilter

Used to preprocess messages. Executes on Imessagerfilter-based objects, messages can be filtered out of the message loop, or specially handled before the message is sent to the message loop

DoEvents

Allow processing of messages in a queue

EnableVisualStyles

Allows you to use XP visual styles for various visual elements of your application. There are two overloaded versions that accept a manifest message. An accepted list stream, a full name and path for receiving the manifest.

Exit and ExitThread

Exit ends all currently running message loops and exits the application. ExitThread only ends the message loop, closing all windows on the current thread.

?

InitializeComponent initializes all the controls that were added to the form, and also initializes the properties of the form.

If any type of code is modified in InitializeComponent, the changes will disappear the next time you make a modification in the designer (VS2008). Each time you make a modification in the designer, the InitializeComponent is regenerated. InitializeComponent is responsible for instantiating the control, and all calls to the referenced control will fail before InitializeComponent.

SuspendLayout: Temporarily suspends the layout event that occurs when the control is first initialized.

ResumeLayout resets the event to a normal state.

?

    1. Control class

The control class is located in the System.Windows.Forms namespace, which is the base class for each control and form. The control class performs core functions to create the interface that the user sees. Derived from the System.ComponentModel.Component class.

?

Size and position

Determined by the height,width,top,bottom,left,right and the supported size and location. Location is a point structure.

The Bounds property returns a Rectangle object that represents a control area. Contains scroll bars and title bars. Rectangle is located in the System.Drawing namespace.

The ClientSize property is a size structure that represents the customer area and does not contain scroll bars and title bars.

Pointtoscreen and ScreenToClient are customer area turn screen coordinates and screen coordinates to customer area. Rectangletoscreen and rectangletoclient have similar functions.

The Dock property determines which edge of the parent control the child control is parked on. DockStyle enumeration values are used for their property values, Top,bottom,right,left,fill and none.

The Anchor property aligns one side of the control with one edge of the parent control. Sets the distance to the edge to a fixed value.

The Anchor property takes the value of the AnchorStyles enumeration, which is top,bottom,right,left,none.

?

User interaction Operations

Common events are click,doubleclick,keydown,keypress,validaing, and paint

?

    1. Standard controls and components

Button control

Derived from the ButtonBase class

Represents a simple command button

You can set the default OK and Cancel buttons in Acceptdefault and CancelDefault of the form.

?

CheckBox control

Derived from the ButtonBase class.

check box

If ThreeState is set to true, there are three states of the check box, denoted by the CheckState property

Checked: Check

Unchecked: Not selected

Indeterminate: check box gray.

Checked property only true or False

?

    1. Form

The form class is derived from the control class

The instantiation and release process of Windows

    • constructor function
    • Load
    • Activated
    • Closing
    • Closed
    • Deactivate

The first three events occur at the time of initialization

Load occurs before the object is instantiated and the form is visible.

The load is thrown, the form is present, but not visible. The form does not yet exist during the execution of the constructor.

If the Visible property is set to true in the constructor or the show method is called. The Load event is immediately raised, which also makes the form visible.

When the form is closed, the closing event occurs when it is shutting down, and the Cancel property is set to True to cancel the shutdown

Closed occurs after the form is closed.

?

The properties associated with the form launch are startposition,showinbartaskbar,topmost.

?

Show and ShowDialog display forms

Show: After execution the form is visible to the user, and subsequent code continues execution

ShowDialog: The code executes after the form is closed. Returns the DialogResult value.

C # Advanced Programming (Sixth Edition) Learning: Chapter 31st: Windows Forms

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.