开发者

.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:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜