简体   繁体   中英

User Control vs. Windows Form

Visual Studio - C# 中的用户控件和 Windows 窗体有什么区别?

Put very simply:

User controls are a way of making a custom, reusable component. A user control can contain other controls but must be hosted by a form.

Windows forms are the container for controls, including user controls. While it contains many similar attributes as a user control, it's primary purpose is to host controls.

They have a lot in common, they are both derived from ContainerControl. UserControl however is designed to be a child window, it needs to be placed in a container. Form was designed to be a top-level window without a parent.

You can actually turn a Form into a child window by setting its TopLevel property to false:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        var child = new Form2();
        child.TopLevel = false;
        child.Location = new Point(10, 5);
        child.Size = new Size(100, 100);
        child.BackColor = Color.Yellow;
        child.FormBorderStyle = FormBorderStyle.None;
        child.Visible = true;
        this.Controls.Add(child);
    }
}

Windows窗体是用户控件的容器。

The biggest difference is form.show gives a different window while usercontrol doesnt have feature like popping up without a parent. Rest things are same in both the controls like beind derived from Scrollablecontrol.

A User Control is a blank control, it's a control that's made up of other controls. Building a user control is similar to building a form. It has a design surface, drag and drop controls onto the design surface, set properties, and events. User controls can consolidate UI and code behind. User controls can only be used in the project where they're defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM