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

最后都变了- 提交于 2019-12-13 15:13:37

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/6708022/net-automation-controltype-document-how-to-manipulate-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!