Microsoft does not provide any slider control in .Net. If we want to have some slider control we need to create our own slider control by using UserControl class.
In this article we discuss about how to create Slider control for C# windows applications. Create new C# windows application and add UserControl by right click on the Solution Explorer, select UserControl => name it as SliderControl.cs.
Now add one Label, one ImageList controls and name them as lblSlider and imgSlider. Add below code for the SliderControl.cs class.
using System;
using System.Windows.Forms;
namespace CSharpCSharpSliderExp
{
public partial class CSharpSliderExp : UserControl
{
private Control m_LowerPanel;
private int FixedHeight = 150;
private int CollapsibleIntervalValue = 2;
private int HeaderHeightValue = 20;
#region Initialize
public CSharpSliderExp()
{
InitializeComponent();
}
#endregion
public Control LowerPanel
{
get
{
return this.m_LowerPanel;
}
set
{
this.m_LowerPanel = value;
}
}
public int SliderInterval
{
get
{
return this.CollapsibleIntervalValue;
}
set
{
this.CollapsibleIntervalValue = value;
}
}
public int HeaderHeight
{
get
{
return this.HeaderHeightValue;
}
set
{
this.HeaderHeightValue = value;
this.lblSlider.Height = this.HeaderHeightValue;
}
}
private void DoSlidable()
{
if (this.m_LowerPanel.Height > this.lblSlider.Height)
{
FixedHeight = this.m_LowerPanel.Height;
while (this.m_LowerPanel.Height > this.lblSlider.Height)
{
Application.DoEvents();
this.m_LowerPanel.Height -= CollapsibleIntervalValue;
}
this.lblSlider.ImageIndex = 1;
this.m_LowerPanel.Height = 0;
}
else if (this.m_LowerPanel.Height < this.lblSlider.Height)
{
int x = this.FixedHeight;
while (this.m_LowerPanel.Height <= (x))
{
Application.DoEvents();
this.m_LowerPanel.Height += CollapsibleIntervalValue;
}
this.lblSlider.ImageIndex = 0;
this.m_LowerPanel.Height = x;
}
}
private void CSharpSliderExp_Load(object sender, EventArgs e)
{
this.FixedHeight = this.Height;
this.lblSlider.Height = this.HeaderHeightValue;
}
private void lblSlider_Click(object sender, EventArgs e)
{
DoSlidable();
}
private void CSharpSliderExp_Resize(object sender, EventArgs e)
{
this.lblSlider.Width = this.Width;
}
}
}
Now drag and drop this control to the Form1 from Components section of Toolbox. Add below code for Form1.cs.
using System;
using System.Windows.Forms;
namespace CSharpSliderExp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cSharpSliderExp1.LowerPanel = panel1;
Label listBox = this.cSharpSliderExp1.Controls["lblSlider"] as Label;
listBox.Text = "Login Form";
}
}
}
Once you run the application, the output is as shown below.