c# - Windows 窗体文本框回车键

标签 c# winforms textbox

我有一个 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 时调用一个方法吗?如果是这样,您必须像这样使用 TextBoxKeyUp 事件:

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/

相关文章:

C#静态方法问题

c# - MonthCalendar 中的奇怪错误突然开始发生

c# - 如何在不加载相关记录的情况下判断是否设置了Entity Framework中的Navigation Property

c# - 具有相同属性名称但大小写不同的 Json 反序列化

c# - 将 Ajax 工具包脚本移动到页面底部

c# - 如何防止 LocalDB 在空闲时间后分离 MDF 文件?

c# - 如何在C#中隐藏或取消隐藏表单

c# - TextBox的OnPaint方法没有被调用?

wpf - XAML 文本框在焦点上更改边框颜色

php - html 表单中 Mysql 数据库中记录的日期值为 0000-00-00