public void SetText(string text, int timeout) { if (AutomationElement.Patterns.Value.IsSupported && AutomationElement.Patterns.Value.TryGetPattern(out var valuePattern)) { AutomationElement.Focus(); ((ValuePattern)valuePattern).SetValue(text); } else if (AutomationElement.Properties.NativeWindowHandle != IntPtr.Zero) { AutomationElement.Focus(); IntPtr wndHandle = AutomationElement.FrameworkAutomationElement.NativeWindowHandle; KeyboardTyper.TypeWithSendInput($"{SpecialChars.KeyBegin}ctrl+home{SpecialChars.KeyEnd}", null, wndHandle, IntPtr.Zero, timeout, false, 0); // Move to start of control KeyboardTyper.TypeWithSendInput($"{SpecialChars.KeyBegin}ctrl+shift+end{SpecialChars.KeyEnd}", null, wndHandle, IntPtr.Zero, timeout, false, 0); // Select everything KeyboardTyper.TypeWithSendInput(text, null, wndHandle, IntPtr.Zero, timeout, false, 0); } else { throw new NotSupportedException("SetText is not supported"); } }
public void SetText(string text, int timeout) { object valuePattern = null; if (automationElement.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern)) { automationElement.SetFocus(); ((ValuePattern)valuePattern).SetValue(text); } else if (automationElement.Current.NativeWindowHandle != 0) { automationElement.SetFocus(); IntPtr wndHandle = new IntPtr(automationElement.Current.NativeWindowHandle); KeyboardTyper.TypeWithSendInput($"{SpecialChars.KeyBegin}ctrl+home{SpecialChars.KeyEnd}", null, wndHandle, IntPtr.Zero, timeout, false, 0); // Move to start of control KeyboardTyper.TypeWithSendInput($"{SpecialChars.KeyBegin}ctrl+shift+end{SpecialChars.KeyEnd}", null, wndHandle, IntPtr.Zero, timeout, false, 0); // Select everything KeyboardTyper.TypeWithSendInput(text, null, wndHandle, IntPtr.Zero, timeout, false, 0); } else { throw new NotSupportedException("SetText is not supported"); } }