Exemplo n.º 1
0
        /// <summary>
        ///     Try to set the text on a control.
        ///     It uses the value pattern and if not available, simulate key press.
        ///     More info at this acdress : http://msdn.microsoft.com/en-us/library/ms750582.aspx
        /// </summary>
        public static bool TrySetText(this WinControl control, string valueToSet)
        {
            //TextPattern and ValuePattern cannnot be used on the same control
            // NOTE: Elements that support TextPattern
            //       do not support ValuePattern and TextPattern
            //       does not support setting the text of
            //       multi-line edit or document controls.
            //       For this reason, text input must be simulated
            //       using one of the following methods.
            //
            if (!control.TrySetValue(valueToSet))
            {
                control.SetFocus();
                Keyboard.SendKeys("^{HOME}"); // Move to start of control
                Keyboard.SendKeys("^+{END}"); // Select everything
                Keyboard.SendKeys("{DEL}");   // Delete selection
                Keyboard.SendKeys(valueToSet);
                return(true);
            }

            return(false);
        }