Winform窗体下,AcceptButton已经设置了Button,TextBox仍能接收回车键

Form.Acceptbutton已经设置了值,一般情况下,TextBox有焦点时,点击回车键就会触发Form.AcceptButton.Click事件。

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class FormDemo : Form
    {
        private TextBox textBox1;
        private Button btnExe;
        private Button btnAccept;

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.btnExe = new System.Windows.Forms.Button();
            this.btnAccept = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 12);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 0;
            // 
            // btnExe
            // 
            this.btnExe.Location = new System.Drawing.Point(132, 12);
            this.btnExe.Name = "btnExe";
            this.btnExe.Size = new System.Drawing.Size(88, 23);
            this.btnExe.TabIndex = 1;
            this.btnExe.Text = "Execute";
            this.btnExe.UseVisualStyleBackColor = true;
            this.btnExe.Click += new System.EventHandler(this.btnExe_Click);
            // 
            // btnAccept
            // 
            this.btnAccept.Location = new System.Drawing.Point(132, 41);
            this.btnAccept.Name = "btnAccept";
            this.btnAccept.Size = new System.Drawing.Size(88, 23);
            this.btnAccept.TabIndex = 2;
            this.btnAccept.Text = "AcceptButton";
            this.btnAccept.UseVisualStyleBackColor = true;
            this.btnAccept.Click += new System.EventHandler(this.btnAccept_Click);
            // 
            // FormDemo
            // 
            this.AcceptButton = this.btnAccept;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.btnAccept);
            this.Controls.Add(this.btnExe);
            this.Controls.Add(this.textBox1);
            this.Name = "FormDemo";
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        public FormDemo()
        {
            InitializeComponent();
        }
        private void btnExe_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Execute按钮点击");
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Accept按钮点击");
        }
    }
}

运行结果如图



但是我们会有这样的需求,我们TextBox数据输入完毕,不想直接执行Form.AcceptButton.Click事件,怎么办?

我们可以利用TextBox.PreviewKeyDown事件。

public FormDemo()
{
    InitializeComponent();
    textBox1.PreviewKeyDown += textBox1_PreviewKeyDown;
}

void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        btnExe.PerformClick();
    }
}

运行结果如图

奇怪两个按钮Click事件都执行了。

但是我们还想屏蔽AcceptButton的Click怎么办?

处理TextBox.PreviewKeyDown:

void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        btnExe.PerformClick();
        e.IsInputKey = true;
    }
}

运行结果如图



好的,我的第一个博文就这样吧。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值