Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Control vs. Windows Form

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

like image 527
Arlen Beiler Avatar asked Feb 19 '10 20:02

Arlen Beiler


People also ask

What is the difference between Windows form and user control?

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.

What is a user control in Windows Forms?

A UserControl is a collection of controls placed together to be used in a certain way. For example you can place a GroupBox that contains Textbox's, Checkboxes, etc. This is useful when you have to place the same group of controls on/in multiple forms or tabs.


2 Answers

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.

like image 76
Bill Martin Avatar answered Oct 18 '22 05:10

Bill Martin


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);     } } 
like image 27
Hans Passant Avatar answered Oct 18 '22 06:10

Hans Passant