开发者

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

开发者 https://www.devze.com 2023-03-20 19:00 出处:网络
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 v

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:

a开发者_运维问答utomationElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern)
    .setValue(value);


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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消