Detect Presence of ListView Scrollbar

G

Guest

Greetings,

Is there a way to know if the vertical scrollbar is visible in a ListView?

In the Details view mode, I would like to set the column widths, so that
there is never a horizontal scrollbar, and there is never whitespace between
the last column and the right side of the ListView. Can this be done
without getting too messy?

Thank you,

Eric
 
A

AlexS

.....
the last column and the right side of the ListView. Can this be done
without getting too messy?
....

No, always mess - fonts, resolutions, screen setup and languages.

However, you can find out client area size and use listview item bounds to
calculate if scrollbar is required. Once again - it is / could be very messy
and not worth the effort. In your case you might consider setting the width
of first - and maybe next - column to some sensible value in Resize event
handler. For example

ColumnHeader ch = listview.Columns[0];
ch.Width=listview.Width/5 * 4; // 80%

HTH
Alex
 
A

Andrew Smith \(Infragistics\)

You can check the window style of the control to see if the vertical
scrollbar is visible but I'm not sure how much that will help you autosize
the columns.

private const int WS_HSCROLL = 0x100000;
private const int WS_VSCROLL = 0x200000;
private const int GWL_STYLE = (-16);

[DllImport("user32", CharSet=CharSet.Auto)]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);

internal static bool IsVerticalScrollBarVisible(Control ctrl)
{
if (!ctrl.IsHandleCreated)
return false;

return (GetWindowLong(ctrl.Handle, GWL_STYLE) & WS_VSCROLL) != 0;
}

internal static bool IsHorizontalScrollBarVisible(Control ctrl)
{
if (!ctrl.IsHandleCreated)
return false;

return (GetWindowLong(ctrl.Handle, GWL_STYLE) & WS_HSCROLL) != 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top