Select Page
Codoid Blog

Setting Value in WPF Textbox

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.

As a continuation to the previous WPF automation article. In this topic, we will see how to set value in WPF Textbox.

Let’s see how it can be implemented using System.Windows.Automation.ValuePattern class.

To explain this implementation, we have used the below application to set the value in Textbox.


'WPF_SetValue function with three arguments
'Argument 1: inWindowHwnd - Window Handle
'Argument 2: inTextBoxIndex - Index of TextBox
'Argument 3: strValue - Value to be set
Function WPF_SetValue(ByVal inWindowHwnd,ByVal inTextBoxIndex,ByVal strValue)
Dim oTreeScope, oAutomationElement,oControlType
Dim oInptr, oPropertyCondition, oValuePattern

'Creating object for ControlType class since the textbox will be identified with Edit Control Type.

Set oControlType=DotNetFactory.CreateInstance("System.Windows.Automation.ControlType","UIAutomationTypes")

'TreeScope object to specify scope of elements

Set oTreeScope=DotNetFactory.CreateInstance("System.Windows.Automation.TreeScope","UIAutomationTypes")

'AutomationElement object is used to retrieve the element

Set oAutomationElement=DotNetFactory.CreateInstance("System.Windows.Automation.AutomationElement","UIAutomationClient")

'PropertyCondition object is used to filter the textbox with ControlType property.

Set oPropertyCondition=DotNetFactory.CreateInstance(" System.Windows.Automation.PropertyCondition","UIAutomationClient",oAutomationElement.ControlTypeProperty,oControlType.Edit)

'Since TextBox control comes under ValuePattern, we need this object to set value in textbox.

Set oValuePattern=DotNetFactory.CreateInstance("System.Windows.Automation.ValuePattern","UIAutomationClient")

'In .Net, Window Handle cannot be taken as Integer or String, it should be passed as IntPtr structure. That is why here inWindowHwnd argument is supplied as constructor for IntPtr object.

Set oInptr=DotNetFactory.CreateInstance("System.IntPtr","",inWindowHwnd)

'Getting the WPF Form using FromHandle method.

Set oAutomationElement=oAutomationElement.FromHandle(oInptr)

'Finding the textbox with Control Type and assigning into oValuePattern object

Set oValuePattern=oAutomationElement.FindAll(oTreeScope.Children,oPropertyCondition)(inTextBoxIndex-1).GetCurrentPattern(oValuePattern.Pattern)

'Setting value in the textbox


Set oTreeScope=Nothing
Set oControlType=Nothing
Set oAutomationElement=Nothing
Set oPropertyCondition=Nothing
Set oValuePattern=Nothing
Set oInptr=Nothing
End Function

Now to set the value in the WPF Textbox, We need to get the window handle for the WPF Application and then call the above function. We pass three arguments to the function:

1.Window Handle
2.Index of the Textbox : Since ‘Email’ is the first textbox available in the window, we pass the index argument value as 1
3.Value to be set in the textbox. Eg. “[email protected]

Dim inWindowHandle
inWindowHandle=Window("text:= ExpenseIt Standalone").GetRoProperty("hwnd")

'Call WPF_SetValue function with Window Handle, textBox Index and value

WPF_SetValue inWindowHandle,1,"[email protected]"


Once the above code is run, the value specified will be set in the textbox. In the above function, the textbox is identified in the Window with the help of ControlTypeProperty.


Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us