Select Page
Codoid Blog

Selecting WPF Checkbox

Being a leading QA Company, we write blogs on all prominent software testing topics and tools using our real-world experience. So stay sharp by subscribing to our Newsletter.

Listen to this blog

In this article, we will see how to select WPF Checkbox using TogglePattern class which is available in UI Automation pattern.

Selecting WPF Checkbox

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)

Submit a Comment

Your email address will not be published.

Talk to our Experts

    Amazing clients who trust us

    image
    image
    image
    image
    image
    image