{"id":77,"date":"2011-04-21T14:58:08","date_gmt":"2011-04-21T04:58:08","guid":{"rendered":"http:\/\/www.reenadu.com\/?p=77"},"modified":"2011-04-21T14:58:08","modified_gmt":"2011-04-21T04:58:08","slug":"create-new-net-component-circularlistbox","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=77","title":{"rendered":"Create customized .Net Component &#8211; CircularListBox in C#"},"content":{"rendered":"<p>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.<\/p>\n<pre lang=\"csharp\">\npublic class CircularListBox : System.Windows.Forms.ListBox\n{\n    private System.ComponentModel.Container components = null;\n    const int WM_VSCROLL = 0x0115;\n    const int WM_KEYDOWN = 0x0100;\n\n    const int SB_LINEUP = 0x0000;\n    const int SB_LINEDOWN = 0x0001;\n\n    const int VK_DOWN = 0x0028;\n    const int VK_UP = 0x0026;\n    const int VK_NEXT = 0x0022;\n    const int VK_PRIOR = 0x0021;\n\n    public CircularListBox()\n    {\n        \/\/ This call is required by the Windows.Forms Form Designer.\n        InitializeComponent();\n\n        \/\/ TODO: Add any initialization after the InitializeComponent call\n\n    }\n\n    \/\/\/ <summary> \n    \/\/\/ Clean up any resources being used.\n    \/\/\/ <\/summary>\n    protected override void Dispose(bool disposing)\n    {\n        if (disposing)\n        {\n            if (components != null)\n            {\n                components.Dispose();\n            }\n        }\n        base.Dispose(disposing);\n    }\n\n    #region Component Designer generated code\n    \/\/\/ <summary> \n    \/\/\/ Required method for Designer support - do not modify \n    \/\/\/ the contents of this method with the code editor.\n    \/\/\/ <\/summary>\n    private void InitializeComponent()\n    {\n        components = new System.ComponentModel.Container();\n    }\n    #endregion\n    protected int offset = 0;\n\n    public new int TopIndex\n    {\n        get { return (this.Items.Count + offset + base.TopIndex) % this.Items.Count; }\n        set\n        {\n            if (value > this.Items.Count - 1)\n                return;\n            Message m = new Message();\n            m.Msg = WM_KEYDOWN;\n            m.WParam = (System.IntPtr)VK_DOWN;\n            while (value != this.TopIndex)\n                WndProc(ref m);\n        }\n    }\n\n    protected override void WndProc(ref Message m)\n    {\n        if (m.Msg == WM_VSCROLL || m.Msg == WM_KEYDOWN)\n        {\n            if (this.SelectedIndex == -1)\n                this.SelectedIndex = 0;\n\n            switch ((int)m.WParam)\n            {\n                case VK_PRIOR:\n                case VK_UP:\n                case SB_LINEUP:\n                    if (this.SelectedIndex == 0)\n                    {\n                        offset = (this.Items.Count + offset - 1) % this.Items.Count;\n                        this.Items.Insert(0, this.Items[this.Items.Count - 1]);\n                        this.Items.RemoveAt(this.Items.Count - 1);\n                        this.SelectedIndex = 0;\n                    }\n                    else\n                    {\n                        this.SelectedIndex--;\n                    }\n                    break;\n                case VK_DOWN:\n                case SB_LINEDOWN:\n                case VK_NEXT:\n                    if (this.SelectedIndex == this.Items.Count - 1)\n                    {\n                        offset = ++offset % this.Items.Count;\n                        this.Items.Add(this.Items[0]);\n                        this.Items.RemoveAt(0);\n                        this.SelectedIndex = this.Items.Count - 1;\n                    }\n                    else\n                    {\n                        this.SelectedIndex++;\n                    }\n                    break;\n                default:\n                    base.WndProc(ref m);\n                    break;\n            }\n        }\n        else\n            base.WndProc(ref m);\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/nickdu.com\/?p=77\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Create customized .Net Component &#8211; CircularListBox in C#&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-net","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=77"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}