Hide vertical scroll bar in ListBox control

前端 未结 2 1568
不思量自难忘°
不思量自难忘° 2021-01-18 06:06

I\'m developing an application that requires a ListBox control. Unfortunately, when I add too many items in the ListBox, a vertical scroll bar is s

2条回答
  •  天涯浪人
    2021-01-18 06:58

    The problem was solved. I've simply created a new project of template a class library with the following code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ClassLibrary1
    {
        public class MyListBox : System.Windows.Forms.ListBox
        {
            private bool mShowScroll;
            protected override System.Windows.Forms.CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
                    if (!mShowScroll)
                        cp.Style = cp.Style & ~0x200000;
                    return cp;
                }
            }
            public bool ShowScrollbar
            {
                get { return mShowScroll; }
                set
                {
                    if (value != mShowScroll)
                    {
                        mShowScroll = value;
                        if (IsHandleCreated)
                            RecreateHandle();
                    }
                }
            }
        }    
    }
    

    Then, I've built the project outputting a new class library ClassLibrary1.dll

    On my main project, I've right-clicked the ToolBox and selected Choose Items.... Clicked on Browse... and selected the class library that I've recently created (ClassLibrary1.dll) and clicked on Open then on OK. Thus, I was able to have my custom ListBox which has no vertical scroll bars anymore.

提交回复
热议问题