Create Floating/Sliding/Moving Menu in C#.NET
Floating/Sliding Menu or Panel in C#.NET
Introduction
Here, I'm going to tell you how I'm creating sliding/floating panel for your Windows application in C#. It's a very simple technique. Try it if you like it...
Using the Code
Follow these steps:
- Start a Windows Form application
- Add a panel (eg:
Panel1
) and dock itTop
- Place two button controls (e.g.:
Button1
,Button2
) inside the panel and dock it to left & right of thePanel1
. - Place another panel (e.g.:
Panel2
) insidePanel1
and setdockstyle
as "Fill
". - Add a "User Control Form" (e.g.:
UserControl1
) to your project and place all your controls on it (e.g.:Panel1,2,3,4,5
&button1,2,3....17
). - Add two
Timer
controls to your project (e.g.:timer1
,timer2
) & setInterval = 5
. - Finally, write the code as shown below:
//
//Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FloatingMenu
{
public partial class Form1 : Form
{
UserControl usrCtrl = new UserControl1(); //Create an instance of UserControl1
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
usrCtrl.Left = usrCtrl.Top = 0; //Set the location of UserControl1
panel2.Controls.Add(usrCtrl); //Adding UserControl1 to Panel2
usrCtrl.Show(); //Shows UserControl1 inside Panel2
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start(); //Enables timer1
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop(); //Disables timer1
}
private void button2_MouseDown(object sender, MouseEventArgs e)
{
timer2.Start(); //Enables timer2
}
private void button2_MouseUp(object sender, MouseEventArgs e)
{
timer2.Stop(); //Disables timer2
}
private void timer1_Tick(object sender, EventArgs e)
{
if (usrCtrl.Left < 0)
{
usrCtrl.Left = usrCtrl.Left + 5; //Move UserControl1 to right side
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (usrCtrl.Right >= panel2.Left + panel2.Width)
{
usrCtrl.Left = usrCtrl.Left - 5; //Move UserControl1 to left side
}
}
}
}
//End of code.
Thank you for using my tricks.
Enjoy programming...