Create customized .Net Component – CircularListBox in C#

This post originated from my previous question. In previous question, I was asking how to create a circular list box control. A list box, if scroll down to the last element and continue scroll down, it will appear first element; if scroll up to the first element and keep scroll up, it will appear last element. It looks like IPhone alarm setting function. Here is my solution.

public class CircularListBox : System.Windows.Forms.ListBox
{
    private System.ComponentModel.Container components = null;
    const int WM_VSCROLL = 0x0115;
    const int WM_KEYDOWN = 0x0100;

    const int SB_LINEUP = 0x0000;
    const int SB_LINEDOWN = 0x0001;

    const int VK_DOWN = 0x0028;
    const int VK_UP = 0x0026;
    const int VK_NEXT = 0x0022;
    const int VK_PRIOR = 0x0021;

    public CircularListBox()
    {
        // This call is required by the Windows.Forms Form Designer.
        InitializeComponent();

        // TODO: Add any initialization after the InitializeComponent call

    }

    ///  
    /// Clean up any resources being used.
    /// 
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code
    ///  
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// 
    private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
    }
    #endregion
    protected int offset = 0;

    public new int TopIndex
    {
        get { return (this.Items.Count + offset + base.TopIndex) % this.Items.Count; }
        set
        {
            if (value > this.Items.Count - 1)
                return;
            Message m = new Message();
            m.Msg = WM_KEYDOWN;
            m.WParam = (System.IntPtr)VK_DOWN;
            while (value != this.TopIndex)
                WndProc(ref m);
        }
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_VSCROLL || m.Msg == WM_KEYDOWN)
        {
            if (this.SelectedIndex == -1)
                this.SelectedIndex = 0;

            switch ((int)m.WParam)
            {
                case VK_PRIOR:
                case VK_UP:
                case SB_LINEUP:
                    if (this.SelectedIndex == 0)
                    {
                        offset = (this.Items.Count + offset - 1) % this.Items.Count;
                        this.Items.Insert(0, this.Items[this.Items.Count - 1]);
                        this.Items.RemoveAt(this.Items.Count - 1);
                        this.SelectedIndex = 0;
                    }
                    else
                    {
                        this.SelectedIndex--;
                    }
                    break;
                case VK_DOWN:
                case SB_LINEDOWN:
                case VK_NEXT:
                    if (this.SelectedIndex == this.Items.Count - 1)
                    {
                        offset = ++offset % this.Items.Count;
                        this.Items.Add(this.Items[0]);
                        this.Items.RemoveAt(0);
                        this.SelectedIndex = this.Items.Count - 1;
                    }
                    else
                    {
                        this.SelectedIndex++;
                    }
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
        else
            base.WndProc(ref m);
    }
}

2 thoughts on “Create customized .Net Component – CircularListBox in C#”

Comments are closed.