C# WinForm Interface Design Tutorial (C# Windows Forms Application)

WinForm is the abbreviation of Windows Form. It is a client (PC software) development technology based on the .NET Framework platform, and generally uses C# programming. C# WinForm programming needs to create a "Windows Forms Application" project.

.NET provides a large number of Windows-style controls and events, which we can use directly. It is easy to use and fast to develop.

Windows Forms application is an important application in C# language, and it is also the most common application of C# language. This set of C# WinForm tutorials will teach you how to use WinForm for interface design, bind corresponding events, and develop a practical client.

For every reader who has used the Windows operating system, Windows applications are no strangers. The Windows application program written in C# language is similar to the interface of the Windows operating system. Each interface is composed of forms, and corresponding functions can be completed through operations such as mouse clicks and keyboard input.

C# creates Windows Forms application (WinForm program)

C# creates Windows Forms application (WinForm program)

In the project folder of each Windows Forms application, there will be a default form program Form1.cs, and the form to be run is specified in the Program.cs file of the project.

The code of the Program.cs file is as follows.

static class Program
{
    
    
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
    
    
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

In the Main method of the above code:
• The first line of code: It is used to start the visual style in the application. If the control and the operating system support it, the drawing of the control can be realized according to the display style.
• Line 2: The control supports the UseCompatibleTextRenderingproperty property, and this method sets this property to the default value.
• The third line of code: used to set the form to be started in the current project, here new Form1() is the form to be started.

Windows Forms applications are also called event-driven programs, that is, click controls on the interface with the mouse, operate controls through keyboard input, etc. to trigger different events of the controls to complete the corresponding operations.

For example, click a button, right-click the interface, enter content into a text box, and so on.

C# set form properties

C# set form properties

Every Windows Forms application is composed of several forms, and the properties in the forms are mainly used to set the appearance of the form.
Common properties of forms are shown in the table below.

Attributes effect
Name Used to get or set the name of the form
WindowState Get or set the window state of the form, there are 3 values, namely Normal (normal), Minimized (minimized), Maximized (maximized), the default is Normal, that is, normal display
StartPosition Get or set the starting position of the form when it is running. There are 5 values, namely Manual (the position of the form is determined by the Location property), CenterScreen (the center of the screen), WindowsDefaultLocation (the default position of Windows), WindowsDefaultBounds (the default position of Windows, boundary Determined by Windows), CenterParent (centered in the parent form), the default is WindowsDefaultLocation
Text Get or set the text in the window title bar
MaximizeBox Get or set whether there is a maximize button in the upper right corner of the form title bar, the default is True
MinimizeBox Gets or sets whether there is a minimize button in the upper right corner of the form title bar, the default is True
BackColor Get or set the background color of the form
BackgroundImage Get or set the background image of the form
BackgroundImageLayout Get or set the image layout, there are 5 values, namely None (the image is displayed on the left), Tile (the image is repeated, the default value), Stretch (stretch), Center (centered), Zoom (scaled to an appropriate size)
Enabled Get or set whether the form is available
Font Get or set the font of the text on the form
ForeColor Get or set the color of the text on the form
Icon Gets or sets the icon displayed on the form

【Example】Create a form named TestForm, and complete the following settings.
• "First Form" appears in the form's title bar.
• The starting position is centered in the form.
• Set a background image on the form.
• The Maximize and Minimize buttons do not appear on forms.

Detailed steps to view the original text

Guess you like

Origin blog.csdn.net/weixin_42715287/article/details/108071065