如何在winform c#中为焦点文本框添加字符串

时间:2011-10-04 07:36:38

标签: c# winforms

我想在winform c#中为焦点文本框添加一个字符。我怎样才能做到这一点 ?实际上我想在e.KeyChar事件中控制myform_KeyPress

private void add_user_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '\r' && hidden_scan_textbox.Text != "")
        {
            shomare_shenasai_view.Text = hidden_scan_textbox.Text;
            hidden_scan_textbox.Text = "";
        }
        if (e.KeyChar != '\r')
        {
            hidden_scan_textbox.Text += e.KeyChar;
       //here i want to add e.KeyChar to focused texbox
            e.KeyChar = '\0';
        }
    }

1 个答案:

答案 0 :(得分:3)

myFormInstance.ActiveControl返回表单中当前的焦点控件 要小心,因为它可能(取决于你的表单的构建方式)也是另一个控件,而不是文本框。

还可以看看这里: What is the preferred way to find focused control in WinForms app?