66

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

0

5 Answers 5

62

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.

2
  • 2
    Can user controls host (contain) other user controls? Apr 30, 2012 at 13:54
  • 2
    @RobertNiestroj yes they can.
    – LxL
    Jan 2, 2014 at 17:43
28

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);
    }
}
3
  • 4
    But you really should avoid doing so, IMO. :) Feb 20, 2010 at 1:57
  • There is a wee bit of memory you'll use needlessly, very small peanuts compared to the cost of the Control class and especially the native window. It would have been very easy for Microsoft to not expose the TopLevel property. The feature is merely obscure, using it is just fine. Aug 2, 2016 at 10:08
  • 1
    I guess I was speaking from the standpoint of code clarity and composition. Sure, you can do this, and sure, it's supported, but I think my point was to not take this good example as a design pattern :) Aug 2, 2016 at 12:10
4

A windows form is a container for user controls.

3
  • how do you put a form into a user control!?
    – Fredou
    Feb 19, 2010 at 20:35
  • 2
    you can't, but you sure can put a user control in a user control. Feb 19, 2010 at 20:35
  • 2
    @Dave - You can, actually. See my post. Feb 19, 2010 at 20:40
2

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.