Panel Control in C#

In this article, we will learn how to use panel in windows form in C#.

Let's create windows form.

Step 1
In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

Step 2
Let's create a panel then you go "View" = > select "ToolBox" (Crtl+Alt+X) = > then select "All Windows Forms" = > Choose "Panel".

create panel
  1. First of all, we'll know about the "panel".

    Panel control helps the user to group a collection of controls. We'll discuss properties and function and events of panel write down.

    "BackColor" changes the background color of panel control.

    panel1.BackColor = Color.Black;
    
    change backcolor panel

    "BorderStyle" changes the style of panel control border.

    panel1.BorderStyle = BorderStyle.FixedSingle;
    
    change boderstyle panel

    "Click" when user mouse click on panel control.

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.Click+=new EventHandler(panel1_Click);
    }
    private void panel1_Click(object sender, EventArgs e)
    {
        Label lbl = new Label();
        lbl.Text = "you click the panel";
        panel1.Controls.Add(lbl);
    }
    
    click panel

    "Cursor" when user mouse hover on panel control then cursor style changed.

    panel1.Cursor = Cursors.Hand;
    

    "Dispose()" released all resources used by the System.ComponentModel.Component.

    panel1.Dispose();
    

    "DoubleClick" when user mouse double click on panel control.

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.DoubleClick+=new EventHandler(panel1_DoubleClick);
    }
    private void panel1_DoubleClick(object sender, EventArgs e)
    {
        Label lbl = new Label();
        lbl.Text = "you double click the panel";
        panel1.Controls.Add(lbl);
    }
    
    double click panel

    "Dock" defines which border of the control are bound to the container.

    panel1.Dock = DockStyle.Top;
    

    6th option available on DockStyle enum.

    DockStyle.Top

    dock top panel

    DockStyle.Right

    dock right panel

    DockStyle.Bottom

    dock bottom panel

    DockStyle.Left

    dock left panel

    DockStyle.Fill

    dock fill panel

    DockStyle.None

    dock none panel

    "Enabled" changes the panel control are enable or not.

    panel1.Enabled = true;
    

    "Focus()" focusing the panel control.

    panel1.Focus();
    

    "Font" changes the style of the text of panel control.

    panel1.Font = new System.Drawing.Font("arial", 14.0F, FontStyle.Bold);
    
    font panel

    "ForeColor" changes the font color of panel control.

    panel1.ForeColor = Color.AliceBlue;
    
    forecolor panel

    "Height" set the height of panel control.

    panel1.Height=200;
    
    height panel

    "Hide()" hides the panel control.

    panel1.Hide();
    

    "MouseClick" when user click the panel by mouse.

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.MouseClick+=new MouseEventHandler(panel1_MouseClick);
    }
    private void panel1_MouseClick(object sender, MouseEventArgs e)
    {
        MessageBox.Show("you mouse click on panel");
    }
    

    "MouseDoubleClick" when user double click the by mouse.

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.MouseDoubleClick += new MouseEventHandler(panel1_MouseDoubleClick);
    }
    private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        MessageBox.Show("you mouse double click on panel");
    }
    

    "MouseEnter" when mouse pointer enters the panel control.

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.MouseEnter += new EventHandler(panel1_MouseEnter);
    }
    private void panel1_MouseEnter(object sender, EventArgs e)
    {
        MessageBox.Show("you mouse pointer enter on panel");
    }
    

    "MouseHover" when mouse pointer rests/hover on the panel control.

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.MouseHover+=new EventHandler(panel1_MouseHover);
    }
    private void panel1_MouseHover(object sender, EventArgs e)
    {
        MessageBox.Show("your mouse hover on panel");
    }
    

    "MouseLeave" when mouse pointer leaves the panel control.

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.MouseLeave+=new EventHandler(panel1_MouseLeave);
    }
    private void panel1_MouseLeave(object sender, EventArgs e)
    {
        MessageBox.Show("your mouse pointer leave on panel");
    }
    

    "MouseMove" when mouse pointer moves the panel control.

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.MouseMove+=new MouseEventHandler(panel1_MouseMove);
    }
    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        MessageBox.Show("your mouse pointer move on panel");
    }
    

    "Padding" sets the padding on panel control.

    panel1.Padding = new Padding(10, 10, 10, 10);
    

    "ResetText()" resets all properties of panel to its default values.

    panel1.ResetText();
    

    "Show()" displays the panel control.

    panel1.Show();
    

    "Visible" gets or sets the panel control visible or not.

    panel.Visible = true;
    


Write a comment


Name Email Message