views:

720

answers:

4

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?

A: 

Call the ShowScrollBar API method.

SLaks
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
What did it return?
SLaks
returned true (means success) but it really didn't work
Nagg
Nagg - Same here...
Adam Haile
Try calling it in the `Layout` or `Resize` events.
SLaks
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
A: 

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
A: 

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);
}
Nagg
Sorry. Fixed source.
Nagg
+3  A: 

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
Works perfect, thank you!
Adam Haile