C# Examples

Menu:


Check Scrollbars Visibility [C#]

Some Windows Forms controls provide an AutoScroll property to specify whether to automatically show scrollbars when the control's content overlaps its visible boundaries. This example demonstates how to find out which scrollbars are visible in a particular control.

Determining the scrollbars visibility

The controls with the auto-scroll functionality provide HorizontalScroll and VerticalScroll properties which enable you to control the automatically shown scrollbars. Use their Visible members to determine whether the scrollbars are visible to the user.

[C#]
ScrollableControl ctl;

ctl.HorizontalScroll.Visible  // the horizontal scrollbar visibility
ctl.VerticalScroll.Visible    // the vertical scrollbar visibility

To demonstrate the properties usage, we will implement a GetVisibleScro­llbars method that returns a ScrollBars value specifying which scrollbars are visible.

[C#]
using System.Windows.Forms;

protected static ScrollBars GetVisibleScrollbars(ScrollableControl ctl)
{
  if (ctl.HorizontalScroll.Visible)
    return ctl.VerticalScroll.Visible ? ScrollBars.Both : ScrollBars.Horizontal;
  else
    return ctl.VerticalScroll.Visible ? ScrollBars.Vertical : ScrollBars.None;
}

An alternative technique

There is an alternative way how to get the scrollbars visibility based on use of the GetWindowLong API function. This technique unlike the foregoing works also on .NET Framework 1.1.

First, create a Win32 class that imports the GetWindowLong function from the user32.dll library and defines some constants.

[C#]
using System.Runtime.InteropServices;

public class Win32
{
  // offset of window style value
  public const int GWL_STYLE = -16;

  // window style constants for scrollbars
  public const int WS_VSCROLL = 0x00200000;
  public const int WS_HSCROLL = 0x00100000;

  [DllImport("user32.dll", SetLastError = true)]
  public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
}

Next, implement the GetVisibleScro­llbars method that calls GetWindowLong to get a control's window style and examines the returned value to get the visible scrollbars. Use the control's Handle property as the first argument of the GetWindowLong method.

[C#]
using System.Windows.Forms;

protected static ScrollBars GetVisibleScrollbars(Control ctl)
{
  int wndStyle = Win32.GetWindowLong(ctl.Handle, Win32.GWL_STYLE);
  bool hsVisible = (wndStyle & Win32.WS_HSCROLL) != 0;
  bool vsVisible = (wndStyle & Win32.WS_VSCROLL) != 0;

  if (hsVisible)
    return vsVisible ? ScrollBars.Both : ScrollBars.Horizontal;
  else
    return vsVisible ? ScrollBars.Vertical : ScrollBars.None;
}

Using the GetVisibleScro­llbars method

You can add a VisibleScrollbars property to your control that indicates which scrollbars are displayed in the control window.

A change of scrollbars visibility causes the cotrol's client size change, and thus, firing the Resize event. Handle this event (e.g. by overriding the OnResize method) to detect scrollbars visibility changes.

[C#]
private ScrollBars _visibleScrollbars = ScrollBars.None;
public ScrollBars VisibleScrollbars
{
  get { return _visibleScrollbars; }
  private set
  {
    if (_visibleScrollbars != value)
    {
      _visibleScrollbars = value;
      OnVisibleScrollbarsChanged(EventArgs.Empty);
    }
  }
}

public event EventHandler VisibleScrollbarsChanged;
protected virtual void OnVisibleScrollbarsChanged(EventArgs e)
{
  if (VisibleScrollbarsChanged != null)
    VisibleScrollbarsChanged(this, e);
}

protected override void OnResize(EventArgs e)
{
  base.OnResize(e);
  VisibleScrollbars = GetVisibleScrollbars(this);
}


See also

By Oto Skrkal, 20-Feb-2009 (updated: 13-Mar-2009)