User Control vs. Windows Form

C#Visual StudioWinforms

C# Problem Overview


What is the difference between a user control and a windows form in Visual Studio - C#?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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);
    }
}

Solution 3 - C#

A windows form is a container for user controls.

Solution 4 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArlen BeilerView Question on Stackoverflow
Solution 1 - C#Bill MartinView Answer on Stackoverflow
Solution 2 - C#Hans PassantView Answer on Stackoverflow
Solution 3 - C#IkkeView Answer on Stackoverflow
Solution 4 - C#sunnytyraView Answer on Stackoverflow