In this article, we will see how to select WPF Checkbox using TogglePattern class which is available in UI Automation pattern.
Code
Function SelectCheckbox(ByVal inWindowHwnd,ByVal strCheckBoxName)
Dim oTreeScope, oAutomationElement,oControlType
Dim oTogglePattern, oInptr, oPropertyCondition
Set oControlType=DotNetFactory.CreateInstance("System.Windows.Automation.ControlType","UIAutomationTypes")
Set oTreeScope=DotNetFactory.CreateInstance("System.Windows.Automation.TreeScope","UIAutomationTypes")
Set oAutomationElement=DotNetFactory.CreateInstance("System.Windows.Automation.AutomationElement","UIAutomationClient")
Set oPropertyCondition=DotNetFactory.CreateInstance("System.Windows.Automation.PropertyCondition","UIAutomationClient",oAutomationElement.NameProperty,strCheckBoxName)
Set oTogglePattern=DotNetFactory.CreateInstance("System.Windows.Automation.TogglePattern","UIAutomationClient")
Set oInptr=DotNetFactory.CreateInstance("System.IntPtr","",inWindowHwnd)
Set oAutomationElement=oAutomationElement.FromHandle(oInptr)
Set oTogglePattern=oAutomationElement.FindAll(oTreeScope.Children,oPropertyCondition)(0).GetCurrentPattern(oTogglePattern.Pattern)
oTogglePattern.Toggle
Set oTreeScope=Nothing
Set oAutomationElement=Nothing
Set oControlType=Nothing
Set oTogglePattern=Nothing
Set oInptr=Nothing
Set oPropertyCondition=Nothing
End Function
Dim inWindowHandle
inWindowHandle=Window("text:=List of Products").GetRoProperty("hwnd")
SelectCheckbox inWindowHandle,"Show only bargains"
Synopsis
Once the above code is executed, “Show only bargains” checkbox will be checked. This “SelectCheckbox” function will check the checkbox if it is already unchecked and vice versa. So it is always recommended to make sure whether the checkbox is checked or not using the “ToggleState” property before calling the function. The ToggleState property for the object can be retrieved as given below.
Msgbox oTogglePattern.current.ToggleState
This implementation for the checkbox is as same as selecting a radio button but the only difference is Toggle Pattern is used for Checkbox.
Comments(0)