What is the difference between a user control and a windows form in Visual Studio - C#?
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.
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.
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With