Reader Level:
ARTICLE

Panel in C#

Posted by Mahesh Chand Articles | Windows Controls C# July 15, 2010
In this article, we will demonstrate how to create and use a Panel Control in a Windows Forms application.
  • 3
  • 0
  • 111223

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.

Design-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.

 PanelImg1.jpg

Figure 1

Run-time

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.

Panel dynamicPanel = new Panel();

 

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.

dynamicPanel.Location = new System.Drawing.Point(26, 12);

dynamicPanel.Name = "Panel1";

dynamicPanel.Size = new System.Drawing.Size(228, 200);

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.

 

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.

PanelImg2.jpg

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.

private void CreateButton_Click(object sender, EventArgs e)

{

    Panel dynamicPanel = new Panel();

    dynamicPanel.Location = new System.Drawing.Point(26, 12);

    dynamicPanel.Name = "Panel1";

    dynamicPanel.Size = new System.Drawing.Size(228, 200);

    dynamicPanel.BackColor = Color.LightBlue;

 

           

    TextBox textBox1 = new TextBox();

    textBox1.Location = new Point(10, 10);

    textBox1.Text = "I am a TextBox5";

    textBox1.Size = new Size(200, 30);

 

    CheckBox checkBox1 = new CheckBox();

    checkBox1.Location = new Point(10, 50);

    checkBox1.Text = "Check Me";

    checkBox1.Size = new Size(200, 30);

 

    dynamicPanel.Controls.Add(textBox1);

    dynamicPanel.Controls.Add(checkBox1);

 

    Controls.Add(dynamicPanel);

}

 

The output looks as in Figure 3.


PanelImg3.jpg

 

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.

dynamicPanel.Visible = false;

 

Summary

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.

COMMENT USING

Trending up