Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
14 Nov 2014 1  
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...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here