Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.70/5 (8 votes)
14 Nov 2014CPOL 43.1K   2.7K   8   3
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...

Image 1

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:
C#
//
//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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Freelance
India India
He is a Commerce Graduate and working as an Accountant. He learned Programming because of his passion and craze. He is a beginner in .NET. His favourite language is C# and favourite database is MySQL.
He loves playing guitar and listening to music.

Comments and Discussions

 
Suggestiongood simple exemple Pin
claudetom0118-Nov-14 11:09
claudetom0118-Nov-14 11:09 
QuestionThx Pin
Tokinabo17-Nov-14 10:21
professionalTokinabo17-Nov-14 10:21 
Thanks for sharing this trick Smile | :)
AnswerRe: Thx Pin
Noble KC17-Nov-14 19:08
professionalNoble KC17-Nov-14 19:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.