Setting Value in WPF Textbox - Codoid
Select Page
Codoid Blog

Setting Value in WPF Textbox

Irrespective of the business that your client is in, marketing is an integral part of their operations. In fact, in this day and age of social.

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.

Code

'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

oValuePattern.SetValue(strValue)

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]"
  

Synopsis

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.

Written By

Submit a Comment

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


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.

Code

'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

oValuePattern.SetValue(strValue)

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]"
  

Synopsis

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.