Working with Multiple Panels in C#.Net or VB.NET Windows Application
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,
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,
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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: