Panel In C#

The Panel Control is a container control to host a group of similar child controls. One of the major uses I have found for a Panel Control is when you need to show and hide a group of controls. Instead of show and hide individual controls, you can simply hide and show a single Panel and all child controls.

In this article, we will demonstrate how to create and use a Panel Control in a Windows Forms application.

Creating a Panel

We can create a Panel Control using the Forms designer at design-time or using the Panel class in code at run-time.


To create a Panel Control at design-time, you can drag and drop a Panel Control from the Toolbox to a Form in Visual Studio. After you dragging and dropping a Panel Control to the Form, the Panel looks like Figure 1. 

Once a Panel is on the form, you can move it around and resize it using the mouse and set its properties and events.

Figure 1


Creating a Panel Control at run-time is merely a work of creating an instance of the Panel class, setting its properties and adding the Panel to the form controls.

The first step to create a dynamic Panel is to create an instance of the Panel class. The following code snippet creates a Panel Control object.

  1. Panel dynamicPanel = newPanel(); 

In the next step, you may set the properties of a Panel Control. 

The following code snippet sets the location, size and Name properties of a Panel.

  1. dynamicPanel.Location = new System.Drawing.Point(26, 12);  
  2. dynamicPanel.Name = "Panel1";  
  3. dynamicPanel.Size = new System.Drawing.Size(228, 200);  
  4. dynamicPanel.TabIndex = 0;  

Once the Panel Control is ready with its properties, the next step is to add the Panel to a form so it becomes a part of the form. 

To do so, we use the "Form.Controls.Add" method that adds the Panel Control to the form's controls and displays it on the form based on the location and size of the control. 

The following code snippet adds a Panel Control to the current form.

  1. Controls.Add(dynamicPanel);  

Setting Panel Properties

After you place a Panel Control on a form, the next step is to set its properties.

The easiest way to set properties is from the Properties Window. You can open the Properties window by pressing F4 or right-clicking on a control and selecting the Properties menu item. The Properties window looks as in Figure 2.

Figure 2

The Panel has most of the common control properties. Here I will discuss the main purpose of a Panel.

Adding Controls to a Panel

You can add controls to a Panel by dragging and dropping a control to the Panel. We can add controls to a Panel at run-time by using its Add method. The following code snippet creates a Panel, creates a TextBox and a CheckBox and adds these two controls to a Panel.

  1. privatevoid CreateButton_Click(object sender, EventArgs e) {  
  2.     Panel dynamicPanel = newPanel();  
  3.     dynamicPanel.Location = new System.Drawing.Point(26, 12);  
  4.     dynamicPanel.Name = "Panel1";  
  5.     dynamicPanel.Size = new System.Drawing.Size(228, 200);  
  6.     dynamicPanel.BackColor = Color.LightBlue;  
  7.     TextBox textBox1 = newTextBox();  
  8.     textBox1.Location = newPoint(10, 10);  
  9.     textBox1.Text = "I am a TextBox5";  
  10.     textBox1.Size = newSize(200, 30);  
  11.     CheckBox checkBox1 = newCheckBox();  
  12.     checkBox1.Location = newPoint(10, 50);  
  13.     checkBox1.Text = "Check Me";  
  14.     checkBox1.Size = newSize(200, 30);  
  15.     dynamicPanel.Controls.Add(textBox1);  
  16.     dynamicPanel.Controls.Add(checkBox1);  
  17.     Controls.Add(dynamicPanel);  
  18. }  

The output looks as in Figure 3.


Show and Hide a Panel

I have seen in many applications that a group of controls on a Form are shown and hidden based on some condition. That is where a Panel is useful. Instead of showing and hidding individual controls, we can group controls that we want to show and hide and place them on two different Panels and show and hide the Panels. To show and hide a Panel, we use the Visible property.

  1. dynamicPanel.Visible = false;  


This article explained how to create a Panel Control in a Windows Forms form at design-time as well as run-time. After that, we saw how to use various properties and methods.

Similar Articles
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.