ATGTestInput.Input.SetValue C# (CSharp) Method

SetValue() public static method

This is a hyrbird of the ValuePattern.SetValue code extracted from the UI Automation sources NOTE: Requires the ability to set focus to the current automation element
public static SetValue ( AutomationElement element, string value ) : void
element System.Windows.Automation.AutomationElement
value string
return void
        public static void SetValue( AutomationElement element, string value )
        {
            IntPtr _hwnd = IntPtr.Zero;

            // Validate arguments / initial setup
            if( value == null )
                throw new ArgumentNullException("string parameter 'value' must not be null");
                
            if( element == null )
                throw new ArgumentNullException("AutomationElement parameter 'element' must not be null");

            // Get hwnd
            _hwnd = GetWindowHandleFromAutomationElement( element );

            // Sanity check #1: Is window enabled???
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            // Sanity check #2: Are there styles that prohibit us sending text to this control?
            NativeMethods.HWND hwnd = NativeMethods.HWND.Cast(_hwnd);
            int WindowStyle = SafeNativeMethods.GetWindowLong(hwnd, SafeNativeMethods.GWL_STYLE);

            if (IsBitSet(WindowStyle, NativeMethods.ES_READONLY))
            {
                throw new InvalidOperationException("Cannot set text to a read-only field");
            }

            // Sanity check #3: Is the size of the text we want to set greater than what the control accepts?
            IntPtr result;
            int resultInt;

            IntPtr resultSendMessage = UnsafeNativeMethods.SendMessageTimeout(hwnd, NativeMethods.EM_GETLIMITTEXT, IntPtr.Zero, IntPtr.Zero, 1, 10000, out result);
            int lastWin32Error    = Marshal.GetLastWin32Error();

            if (resultSendMessage == IntPtr.Zero)
            {
                throw new InvalidOperationException("SendMessageTimeout() timed out");
            }
            resultInt = unchecked((int)(long)result);

            // A result of -1 means that no limit is set.
            if (resultInt != -1 && resultInt < value.Length)
            {
                throw new InvalidOperationException("Length of text (" + value.Length + ") is greater than upper limit of control (" + resultInt.ToString() + ")");
            }

            // Send the message...!
            result = UnsafeNativeMethods.SendMessageTimeout(hwnd, NativeMethods.WM_SETTEXT, IntPtr.Zero, new StringBuilder(value), 1, 10000, out result);
            resultInt = unchecked((int)(long)result);
            if (resultInt != 1)
            {
                throw new InvalidOperationException("Unable to set the text of the control, text = " + value );
            }
        }