Pages

Wednesday, October 21, 2009

Check Scrollbars Visibility [C#]

ome 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;


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#]


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#]

Snippet (programming)

Snippet is a programming term for a small region of re-usable source code, machine code or text. Ordinarily, these are formally-defined operative units to incorporate into larger programming modules. Snippets are often used to clarify the meaning of an otherwise "cluttered" function, or to minimize the use of repeated code that is common to other functions.

Snippet management is a feature of some text editors, program source code editors, IDEs, and related software. It allows the user to persist and use snippets in the course of routine edit operations.