Hide vertical scroll bar in ListBox control

好久不见. 提交于 2019-12-01 16:41:45
Picrofo Software

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.

Except from the horizontal scroll-bar, there is no way with normal use you can turn off the vertical scroll-bar.

You can only set it always visible or auto using the property ScrollAlwaysVisible (also in VB).

When you add item you can instead re-calculate ClientSize by calculating, something like this (untested, you might need to add Padding values to it as well):

 Size sz = new Size(ListBox1.ClientSize.Width, _
                    ListBox1.Items.Count * ListBox1.Font.Height);
 ListBox1.ClientSize = sz

Of course, you should add check to the value in case it is zero, and/or you want a minimum/maximum height.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!