我有一个 TextBox,但我找不到任何解释按下按钮时如何调用函数的资源。
public Simple()
{
Text = "Server Command Line";
Size = new Size(800, 400);
CenterToScreen();
Button button = new Button();
TextBox txt = new TextBox ();
txt.Location = new Point (20, Size.Height - 70);
txt.Size = new Size (600, 30);
txt.Parent = this;
button.Text = "SEND";
button.Size = new Size (50, 20);
button.Location = new Point(620, Size.Height-70);
button.Parent = this;
button.Click += new EventHandler(Submit);
}
一些消息来源告诉我使用一个函数,但我不明白它是如何被调用的。
最佳答案
如果我理解正确,你想在用户在文本框中输入任何内容时按下 Enter
时调用一个方法吗?如果是这样,您必须像这样使用 TextBox
的 KeyUp
事件:
public Simple()
{
Text = "Server Command Line";
...
TextBox txt = new TextBox ();
txt.Location = new Point (20, Size.Height - 70);
txt.Size = new Size (600, 30);
txt.KeyUp += TextBoxKeyUp; //here we attach the event
txt.Parent = this;
Button button = new Button();
...
}
private void TextBoxKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Do something
e.Handled = true;
}
}
关于c# - Windows 窗体文本框回车键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18059306/