In this post we will see the small example of working with multiple panels in windows c# applications.
We will also see how to make the panels visible or invisible, change the location of a panel control programmatically at runtime .

for this, I am designing the form as shown in the image below,
Multiple Panels winforms
Multiple Panels Window Design

The form contains 3 panels panel1,panel3 and panel3 each panel contains a button and label in it.

In the button click code i am making the panels visible or invisible and changing the location of panels.
 
The c# code in the code behind file is as follows :
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Point loc = new Point(94, 146); //new location point
        private void Form1_Load(object sender, EventArgs e)
        {
            panel1.Visible = true;
            panel1.Location = loc; //changing location of panel1
            panel2.Visible = false;
            panel3.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = true;
            panel2.Location = loc; //changing location of panel2
            panel3.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = false;
            panel3.Visible = true;
            panel3.Location = loc; //changing location of panel3
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            panel3.Visible = false;
            panel2.Visible = false;
            panel1.Visible = true;
            panel1.Location = loc; //changing location of panel1
        }


    }
}

The VB.NET code in the code behind file is as follows:
Public Class Form1
    Dim loc As Point = New Point(94, 146) 'new location point

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        panel1.Visible = True
        panel1.Location = loc 'changing location of panel1
        panel2.Visible = False
        panel3.Visible = False
    End Sub
    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        panel1.Visible = False
        panel2.Visible = True
        panel2.Location = loc 'changing location of panel2
        panel3.Visible = False
    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        panel1.Visible = False
        panel2.Visible = False
        panel3.Visible = True
        panel3.Location = loc 'changing location of panel3
    End Sub

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
        panel3.Visible = False
        panel2.Visible = False
        panel1.Visible = True
        panel1.Location = loc 'changing location of panel1
    End Sub

End Class

Output:

Multiple Panels winforms demo
Demo : Multiple Panels winforms 

Leave a Reply

Your email address will not be published.