Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Automation ControlType.Document: how to manipulate text?

How can I set text into a ControlType.Document element using the System.Windows.Automation?

The ValuePattern is not available for Document ControlType and TextPattern doesn't allow setting of new values.

This does not work:

automationElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern)
    .setValue(value);
like image 448
powtac Avatar asked Dec 22 '25 05:12

powtac


1 Answers

I found an ugly way with this method:

private void InsertTextIntoAutomationElement(AutomationElement element, string value) {

    object valuePattern = null;

    if (!element.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern)) {
        element.SetFocus();
        Thread.Sleep(100);

        SendKeys.SendWait("^{HOME}");   // Move to start of control
        SendKeys.SendWait("^+{END}");   // Select everything
        SendKeys.SendWait("{DEL}");     // Delete selection
        SendKeys.SendWait(value);
    } else{
        element.SetFocus();
        ((ValuePattern)valuePattern).SetValue(value);
    }
}
like image 152
powtac Avatar answered Dec 23 '25 19:12

powtac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!