.NET Automation ControlType.Document:如何处理文本? [英] .NET Automation ControlType.Document: how to manipulate text?

查看:150
本文介绍了.NET Automation ControlType.Document:如何处理文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用System.Windows.Automation将文本设置为ControlType.Document元素?

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

ValuePattern不适用于Document ControlType,而TextPattern不允许设置新值.

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

这不起作用:

automationElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern)
    .setValue(value);

推荐答案

我发现此方法很丑陋:

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

这篇关于.NET Automation ControlType.Document:如何处理文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆