Export (0) Print
Expand All

ScrollPattern.SetScrollPercent Method

Sets the horizontal and/or vertical scroll position as a percentage of the total content area within the AutomationElement.

Namespace:  System.Windows.Automation
Assembly:  UIAutomationClient (in UIAutomationClient.dll)

public void SetScrollPercent(
	double horizontalPercent,
	double verticalPercent
)

Parameters

horizontalPercent
Type: System.Double

The percentage of the total horizontal content area. NoScroll should be passed in if the control cannot be scrolled in this direction.

verticalPercent
Type: System.Double

The percentage of the total vertical content area. NoScroll should be passed in if the control cannot be scrolled in this direction.

ExceptionCondition
ArgumentException

A value that cannot be converted to a double is passed in.

ArgumentOutOfRangeException

A value greater than 100 or less than 0 is passed in (except -1, which is equivalent to NoScroll). The HorizontalScrollPercent and VerticalScrollPercent values are normalized to either 0% or 100%.

InvalidOperationException

An attempt is made to scroll in an unsupported direction.

This method is only useful when the content area of the control is larger than the visible region.

Passing in the value NoScroll indicates that there is no scrolling in the specified direction.

In the following example, a ScrollPattern control pattern is obtained from an AutomationElement and is then used to scroll the viewable region to the top left 'home' position of the content area.

///-------------------------------------------------------------------- 
/// <summary> 
/// Obtains a ScrollPattern control pattern from an  
/// automation element. 
/// </summary> 
/// <param name="targetControl">
/// The automation element of interest. 
/// </param> 
/// <returns> 
/// A ScrollPattern object. 
/// </returns> 
///-------------------------------------------------------------------- 
private ScrollPattern GetScrollPattern(
    AutomationElement targetControl)
{
    ScrollPattern scrollPattern = null;

    try
    {
        scrollPattern =
            targetControl.GetCurrentPattern(
            ScrollPattern.Pattern)
            as ScrollPattern;
    }
    // Object doesn't support the ScrollPattern control pattern 
    catch (InvalidOperationException)
    {
        return null;
    }

    return scrollPattern;
}


...


///-------------------------------------------------------------------- 
/// <summary> 
/// Obtains a ScrollPattern control pattern from an automation  
/// element and attempts to scroll to the 'home' position. 
/// </summary> 
/// <param name="targetControl">
/// The automation element of interest. 
/// </param> 
///-------------------------------------------------------------------- 
private void ScrollHome(AutomationElement targetControl)
{
    if (targetControl == null)
    {
        throw new ArgumentNullException(
            "AutomationElement argument cannot be null.");
    }

    ScrollPattern scrollPattern = GetScrollPattern(targetControl);

    if (scrollPattern == null)
    {
        return;
    }

    try
    {
        scrollPattern.SetScrollPercent(0, 0);
    }
    catch (InvalidOperationException)
    {
        // Control not able to scroll in the direction requested; 
        // when scrollable property of that direction is False 
        // TO DO: error handling.
    }
    catch (ArgumentOutOfRangeException)
    {
        // A value greater than 100 or less than 0 is passed in  
        // (except -1 which is equivalent to NoScroll). 
        // TO DO: error handling.
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft