Hide vertical scrollbar in a ListBox control

I am developing an application that requires a ListBox control. Unfortunately, when I add too many elements to the ListBox , a vertical scrollbar is displayed. Is there something I can do to hide the vertical scrollbar displayed by the ListBox ? I see that there is a property to hide the horizontal scrollbar, but there is no property for the vertical scrollbar.

+6
source share
2 answers

The problem is resolved. I just created a new class library template project 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 built a project that outputs the new ClassLibrary1.dll class ClassLibrary1.dll

In my main project, I right-click the ToolBox and select Choose Items... Clicking Browse ... and selected the newly created class library (ClassLibrary1.dll) and clicked Open and then OK . That way I was able to create my own ListBox that no longer has vertical scrollbars.

+8
source

With the exception of the horizontal scrollbar, normal use is not possible; you can disable the vertical scrollbar.

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

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

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

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

+1
source

All Articles