UI Automation - Set Text for a another application's TextBox

Viewed 2326

I have two forms. When a button is clicked in one of them, I want to open the other one and fill a textbox in it. I tried with the code below but it gives an error that says "Unsupported Pattern".

Here is my code:

private void button1_Click(object sender, EventArgs e)
{
    string automationId = "Form1";
    string newTextBoxValue = "user1";
    var condition = new PropertyCondition(AutomationElement.AutomationIdProperty, automationId);
    var textBox = AutomationElement.RootElement.FindFirst(TreeScope.Subtree, condition);
    ValuePattern vPattern = (ValuePattern)textBox.GetCurrentPattern(ValuePattern.Pattern);
    vPattern.SetValue(newTextBoxValue);
}
2 Answers

You should first check for the availability of the ValuePattern pattern:

  • If the ValuePattern pattern is available, use its SetValue method.
  • Else use one of the following solutions:
    1. Set the focus to the control and use SendKeys to clear and set the text.
    2. Or Use SendMessage and send WM_SETTEXT message to set the text,

Example

var notepad = System.Diagnostics.Process.GetProcessesByName("notepad").FirstOrDefault();
if (notepad != null)
{
    var root = AutomationElement.FromHandle(notepad.MainWindowHandle);
    var element = root.FindAll(TreeScope.Subtree, Condition.TrueCondition)
                        .Cast<AutomationElement>()
                        .Where(x => x.Current.ClassName == "Edit" &&
                                    x.Current.AutomationId == "15").FirstOrDefault();
    if (element != null)
    {
        if (element.TryGetCurrentPattern(ValuePattern.Pattern, out object pattern))
        {
            ((ValuePattern)pattern).SetValue("Something!");
        }
        else
        {
            element.SetFocus();
            SendKeys.SendWait("^{HOME}");   // Move to start of control
            SendKeys.SendWait("^+{END}");   // Select everything
            SendKeys.SendWait("{DEL}");     // Delete selection
            SendKeys.SendWait("Something!");

           // OR 
           // SendMessage(element.Current.NativeWindowHandle, WM_SETTEXT, 0, "Something!");
        }
    }
}

In case of using SendMessage make sure you add the following declarations to the class:

[System.Runtime.InteropServices.DllImport("User32.dll")]
static extern int SendMessage(int hWnd, int uMsg, int wParam, string lParam);
const int WM_SETTEXT = 0x000C;

You can read about the approach:

First you should get a handle for the second form you want to open. If it is previously created and stored as a class variable then use it, otherwise create it in this method and open it.

In order for you to be able to fill a textbox in another form, you need to set it's accessor as public, or create a public setter method for it.

private void button1_Click(object sender, EventArgs e)
{
    string automationId = "Form1";
    string newTextBoxValue = "user1";
    var condition = new PropertyCondition(AutomationElement.AutomationIdProperty, automationId);
    var textBox = AutomationElement.RootElement.FindFirst(TreeScope.Subtree, condition);
    ValuePattern vPattern = (ValuePattern)textBox.GetCurrentPattern(ValuePattern.Pattern);
    vPattern.SetValue(newTextBoxValue);

    // this is the idea, not tested, adjust it to yourself
    var form2 = new SecondForm();
    form2.YourTextBox.Text = newTextBoxValue;
    form2.Show();
}
Related