UI 自动化 - 为另一个应用程序的 TextBox 设置文本

我有两种形式。当单击其中一个按钮时,我想打开另一个按钮并在其中填充一个文本框。我尝试使用下面的代码,但它给出了一个错误,上面写着“不支持的模式”。


这是我的代码:


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);

}


慕桂英4014372
浏览 135回答 2
2回答

RISEBY

您应该首先检查ValuePattern模式的可用性:如果ValuePattern模式可用,请使用其SetValue方法。否则使用以下解决方案之一:将焦点设置在控件上,并用于SendKeys清除和设置文本。或使用SendMessage并发送WM_SETTEXT消息来设置文本,例子var notepad = System.Diagnostics.Process.GetProcessesByName("notepad").FirstOrDefault();if (notepad != null){&nbsp; &nbsp; var root = AutomationElement.FromHandle(notepad.MainWindowHandle);&nbsp; &nbsp; var element = root.FindAll(TreeScope.Subtree, Condition.TrueCondition)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Cast<AutomationElement>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(x => x.Current.ClassName == "Edit" &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.Current.AutomationId == "15").FirstOrDefault();&nbsp; &nbsp; if (element != null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (element.TryGetCurrentPattern(ValuePattern.Pattern, out object pattern))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((ValuePattern)pattern).SetValue("Something!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element.SetFocus();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendKeys.SendWait("^{HOME}");&nbsp; &nbsp;// Move to start of control&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendKeys.SendWait("^+{END}");&nbsp; &nbsp;// Select everything&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendKeys.SendWait("{DEL}");&nbsp; &nbsp; &nbsp;// Delete selection&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendKeys.SendWait("Something!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// OR&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// SendMessage(element.Current.NativeWindowHandle, WM_SETTEXT, 0, "Something!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}如果使用SendMessage,请确保将以下声明添加到类中:[System.Runtime.InteropServices.DllImport("User32.dll")]static extern int SendMessage(int hWnd, int uMsg, int wParam, string lParam);const int WM_SETTEXT = 0x000C;

SMILET

首先,您应该获得要打开的第二个表单的句柄。如果它先前已创建并存储为类变量,则使用它,否则在此方法中创建并打开它。为了让您能够在另一个表单中填充文本框,您需要将其访问器设置为公共,或为其创建公共设置器方法。private void button1_Click(object sender, EventArgs e){&nbsp; &nbsp; string automationId = "Form1";&nbsp; &nbsp; string newTextBoxValue = "user1";&nbsp; &nbsp; var condition = new PropertyCondition(AutomationElement.AutomationIdProperty, automationId);&nbsp; &nbsp; var textBox = AutomationElement.RootElement.FindFirst(TreeScope.Subtree, condition);&nbsp; &nbsp; ValuePattern vPattern = (ValuePattern)textBox.GetCurrentPattern(ValuePattern.Pattern);&nbsp; &nbsp; vPattern.SetValue(newTextBoxValue);&nbsp; &nbsp; // this is the idea, not tested, adjust it to yourself&nbsp; &nbsp; var form2 = new SecondForm();&nbsp; &nbsp; form2.YourTextBox.Text = newTextBoxValue;&nbsp; &nbsp; form2.Show();}
打开App,查看更多内容
随时随地看视频慕课网APP