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 it Top
- Place two button controls (e.g.:
Button1
, Button2
) inside the panel and dock it to left & right of the Panel1
.
- Place another panel (e.g.:
Panel2
) inside Panel1
and set dockstyle
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
) & set Interval = 5
.
- Finally, write the code as shown below:
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();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
usrCtrl.Left = usrCtrl.Top = 0; panel2.Controls.Add(usrCtrl); usrCtrl.Show(); }
private void button1_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start(); }
private void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop(); }
private void button2_MouseDown(object sender, MouseEventArgs e)
{
timer2.Start(); }
private void button2_MouseUp(object sender, MouseEventArgs e)
{
timer2.Stop(); }
private void timer1_Tick(object sender, EventArgs e)
{
if (usrCtrl.Left < 0)
{
usrCtrl.Left = usrCtrl.Left + 5; }
}
private void timer2_Tick(object sender, EventArgs e)
{
if (usrCtrl.Right >= panel2.Left + panel2.Width)
{
usrCtrl.Left = usrCtrl.Left - 5; }
}
}
}
Thank you for using my tricks.
Enjoy programming...