65.9K
CodeProject is changing. Read more.
Home

Create Floating/Sliding/Moving Menu in C#.NET

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.70/5 (8 votes)

Nov 14, 2014

CPOL
viewsIcon

44236

downloadIcon

2760

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:

  1. Start a Windows Form application
  2. Add a panel (eg: Panel1) and dock it Top
  3. Place two button controls (e.g.: Button1, Button2) inside the panel and dock it to left & right of the Panel1.
  4. Place another panel (e.g.: Panel2) inside Panel1 and set dockstyle as "Fill".
  5. 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).
  6. Add two Timer controls to your project (e.g.: timer1, timer2) & set Interval = 5.
  7. 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...