Jeffrey L. Roberts
Jeffrey L. Roberts

Reputation: 2984

AutomationElement ScrollBar throws Access Violation on ScrollVertical

I am attempting to use the following code to scroll a scrollbar in a third party .net application. When I run the code in visual studio, it throws an access violation. When I execute the assembly outside visual studio, it says Unsupported Pattern.... Any ideas are greatly appreciated =]

if(child.Current.ClassName == "ScrollBar")
{
    PropertyCondition condition = new PropertyCondition(AutomationElement.AutomationIdProperty, child.Current.AutomationId);
    AutomationElement btnElement = child.FindFirst(TreeScope.Element, condition);
    ScrollPattern btnPattern = btnElement.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern;
    btnPattern.ScrollVertical(ScrollAmount.LargeIncrement);
}

Upvotes: 1

Views: 484

Answers (1)

Ronak Agrawal
Ronak Agrawal

Reputation: 1066

Few checks that i would have done:

  1. Null check missing for your btnElement.
  2. Make sure Scroll bar is actually present(Scroll pattern is not exposed unless pane size exceeds window size-if designed that way).
  3. lastly, instead of directly getting pattern as ScrollPattern, try GetAllSupportedPatterns from bthElement, make sure Scroll Pattern is actually present.

Upvotes: 2

Related Questions