12

I've got a ListView control in Details mode with a single column. It's on a form that is meant to only be used with the keyboard, mostly with the up/down arrows for scrolling and enter to select. So I don't really need to have the scroll bars and would just like them to not show for a cleaner look. However, when I set the ListView.Scrollable property to false, I can still move the selected item up and down, but as soon as it moves to an item not currently in view, the list won't move to show that item. I've tried using EnsureVisible to programmatically scroll the list, but it does nothing when in this mode.

Is there any way to manually move the list up and down to scroll, but without having the scrollbar present?

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Adam Haile
  • 30,705
  • 58
  • 191
  • 286

6 Answers6

18

It's not easy but it can be done. If you try to hide the scroll bar through ShowScrollBar, the ListView will simply put it back again. So you have to do something more devious.

You will have to intercept the WM_NCCALCSIZE message, and in there, turn off the vertical scroll style. Whenever the listview tries to turn it on again, you will turn it off again in this handler.

public class ListViewWithoutScrollBar : ListView
{
    protected override void WndProc(ref Message m) {
        switch (m.Msg) {
            case 0x83: // WM_NCCALCSIZE
                int style = (int)GetWindowLong(this.Handle, GWL_STYLE);
                if ((style & WS_VSCROLL) == WS_VSCROLL)
                    SetWindowLong(this.Handle, GWL_STYLE, style & ~WS_VSCROLL);
                base.WndProc(ref m);
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }
    const int GWL_STYLE = -16;
    const int WS_VSCROLL = 0x00200000;

    public static int GetWindowLong(IntPtr hWnd, int nIndex) {
        if (IntPtr.Size == 4)
            return (int)GetWindowLong32(hWnd, nIndex);
        else
            return (int)(long)GetWindowLongPtr64(hWnd, nIndex);
    }

    public static int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong) {
        if (IntPtr.Size == 4)
            return (int)SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
        else
            return (int)(long)SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
    }

    [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
    public static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)]
    public static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
    public static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
    public static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, int dwNewLong);
}

This will give you a ListView without scroll bars that still scrolls when you use the arrow keys to change selection.

Grammarian
  • 6,774
  • 1
  • 18
  • 32
  • I tried the same code for horizontal scroll (I replaced 0x00200000 by 0x00100000) , the scrollbar is hided but I cannot scroll with arrow keys. Any idea? – Sacha Feb 21 '17 at 10:15
  • This code works fine for regular rows, but the header looks wrong, it doesn't seem to redraw the areas where the scrollbar is overlayed. BTW, I'm using ObjectListView, which is a subclass of Listview. – Gerardo Contijoch Apr 13 '21 at 15:32
1

i did something more easy. i left scrollable to true and used a custom slider(colorSlider) that i found on codeproject and i drawed the slider over the position where the vscroller would appear and then used the ensureVisible function.

visar1990
  • 11
  • 1
0

You can use ListView.Scrollable Property. Set it to false and Scroll bars won't appear!

0

Call the ShowScrollBar API method.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Used [DllImport("user32.dll")] static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow); With: ShowScrollBar(lstMain.Handle, 3, false); And it didn't work... – Adam Haile Mar 21 '10 at 20:51
  • Try calling it in the `Layout` or `Resize` events. – SLaks Mar 21 '10 at 21:12
  • Also, see here: http://www.windowsdevelop.com/windows-forms-general/prevent-listview-scrollbars-from-display-26889.shtml and here: http://stackoverflow.com/questions/159864/synchronized-listviews-in-net – SLaks Mar 21 '10 at 21:14
  • ShowScrollBar API works fine if you set it when WM_NCCALCSIZE is received on WndProc(). But the scrolling functionality is lost. – Gerardo Contijoch Jun 22 '21 at 16:56
0

If ShowScrollBar doesn't work, I'm not sure how to do it.

You could put the ListView in a panel and make the ListView wider than the panel so that the scrollbar is cut off (check SystemInformation.VerticalScrollBarWidth), but that's a horrifyingly ugly hack.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Since ShowScrollBar didn't work, maybe this helps:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);

private const int WM_VSCROLL = 0x115;
private const int SB_LINEDOWN = 1;

public Form1()
{
    InitializeComponent();
    for (int i = 0; i < 100; i++)
        listView1.Items.Add("foo" + i);
    listView1.Scrollable = false;
}

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    SendMessage(listView1.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
}
EgorBo
  • 6,120
  • 3
  • 35
  • 40