使用SendKeys在c#中发送数据时来自文本框的输出不正确

问题描述 投票:0回答:1

我正在开发一个winForm应用程序,它使用sendkeys将值发送到不同应用程序(java应用程序)的文本框中。

下面是我的代码

    SendKeys.Send("abcd".ToUpper().Trim());
    Thread.Sleep(50);
    //it will send tab key to focus next textbox
    SendKeys.Send("{TAB}");
    Thread.Sleep(50);
    SendKeys.Send("efgh".ToUpper().Trim());
    //send tab to get focus on button
    SendKeys.Send("{TAB}");
    // it will send key "Enter" for mouse click
    SendKeys.Send("{ENTER}");

但sendKeys正在向文本框发送错误的数据。欲望输出应该是abcd到textbox1和efgh到textbox2。但有时候我会向texbox1和fgh转到textbox2。它合并输入值。有谁知道为什么会这样?请让我知道如何解决这个问题。

c# winforms sendkeys
1个回答
0
投票
  1. 确保正确配置TabIndex属性。例如App
  2. 单击发送键按钮[TabIndex:0]我添加了一个Tab,将焦点导航到control中的下一个TextBox1 [focus to form],然后完成你想要实现的目标。 private void btnSendKey_Click(object sender, EventArgs e) { SendKeys.Send("{TAB}"); Thread.Sleep(50); SendKeys.Send("abcd".ToUpper().Trim()); Thread.Sleep(50); //it will send tab key to focus next textbox SendKeys.Send("{TAB}"); Thread.Sleep(50); SendKeys.Send("efgh".ToUpper().Trim()); //send tab to get focus on button SendKeys.Send("{TAB}"); // it will send key "Enter" for mouse click SendKeys.Send("{ENTER}"); }

在输入键上单击仅显示值

    private void btnEnter_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Text Box 1: " + textBox1.Text.ToString() + " - Text Box 2:" + textBox2.Text.ToString());
    }`
© www.soinside.com 2019 - 2024. All rights reserved.