Question

is there a way I can stop the horizontal scroll bar from ever showing up in a listview? I want the vertical scroll bar to show when needed but I want the horizontal scroll bar to never show up.

I would imagine it would have something to do with WndProc?

Thanks

Was it helpful?

Solution

You could try something like this, I used in a project once and it worked:

[DllImport ("user32")]
private static extern long ShowScrollBar (long hwnd , long wBar, long bShow);
long SB_HORZ = 0;
long SB_VERT = 1;
long SB_BOTH = 3;

private void HideHorizontalScrollBar ()
{
    ShowScrollBar(listView1.Handle.ToInt64(), SB_HORZ, 0);
}

Hope it helps.

OTHER TIPS

There is a much simpler way to eliminate the lower scroll bar and have the vertical showing. It consists of making sure the header and if no header the rows are the width of the listview.Width - 4 and if the vertical scroll bar is show then listview.Width - Scrollbar.Width - 4;

the following code demostrates how to:

lv.Columns[0].Width = Width - 4 - SystemInformation.VerticalScrollBarWidth;

@bennyyboi's answer is unsafe, as it unbalances the stack. you should use the following code instead for the DllImport:

[System.Runtime.InteropServices.DllImport("user32", CallingConvention=System.Runtime.InteropServices.CallingConvention.Winapi)]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]

private static extern bool ShowScrollBar(IntPtr hwnd, int wBar, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] bool bShow);

Andreas Reiff covers this in his comment above after looking again, so I guess here it is all nicely formatted.

The best solution is the accepted answer that was given here: How to hide the vertical scroll bar in a .NET ListView Control in Details mode

It works perfectly and you don't need some tricks like column width adjustments. Moreover you disable the scrollbar right when you create the control.

The drawback is that you have to create your own list view class which derives from System.Windows.Forms.ListView to override WndProc. But this is the way to go.

To disable the horizontal scroll bar, remember to use WS_HSCROLL instead of WS_VSCROLL (which was used in the linked answer). The value for WS_HSCROLL is 0x00100000.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top