单击输入按钮以创建新的文本框 [英] Click an enter button to create a new textbox
问题描述
如何通过单击输入按钮创建一个新的 TextBox
?以及如何将光标聚焦在新文本框上(每次创建新文本框时,光标都会聚焦在最后一个文本框上)?
How do I create a new TextBox
by clicking an enter button? And how do I focus the cursor on the new textbox (the cursor focus on the last textbox every time the new textbox has been created)?
我已经尝试过这个代码:
I has been tried this code:
"private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Enter += new EventHandler(textBox1_Enter);
}
private void textBox1_Enter(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Size = new Size(100, 50);
tb.Location = new Point(100, 100);
Controls.Add(tb);
}"
但是当我按下回车按钮时它不会创建一个新的文本框
But it not create a new textbox when i press an enter button
推荐答案
加载表单时:
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
然后
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
TextBox textbox = new TextBox();
this.Controls.Add(textbox);
textbox.Focus();
}
}
本质上,创建在按下某个键时触发的事件.如果键是返回键,则创建文本框.
Essentially, create the event that fires when a key is pressed. If the key is the return key, create the textbox.
您的示例代码实际上没有意义,因为您是从文本框中触发事件,而您希望它们从表单中触发.
Your sample code doesn't really make sense because you are firing events from a textbox, when you want them fired from the form.
这篇关于单击输入按钮以创建新的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!