C# ListView Disable Horizontal Scrollbar

风格不统一 提交于 2019-12-01 03:55:30

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.

Dean McCoy

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.

Robert S.

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.

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