在Form AcceptButton之前不会引发TextBox KeyDown事件按下' Enter'键

时间:2018-03-16 01:08:39

标签: c# winforms

我在VS2015中创建了一个Winform应用程序。一个名为Form1的新表单,其中一个TextBox名为' TextBox1'和一个名为'按钮1'在里面。

Form1.AcceptButton是button1,将keyDown事件绑定到TextBox。 TextBox得到专注,我按“输入”#39;键,TextBox1 keyDown事件未引发。

预期:引发TextBox1 KeyDown事件,然后按钮1单击事件引发。

Form1.cs的

    public Form1()
    {
        InitializeComponent();
    }
    //protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    //{
    //    MessageBox.Show("ProcessCmdKey");
    //    return base.ProcessCmdKey(ref msg, keyData);
    //}

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        MessageBox.Show("TextBox KeyDwon");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Button Click");
    }

Form1.Desiger.cs

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(74, 42);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 20);
        this.textBox1.TabIndex = 0;
        this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(197, 42);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 1;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // Form1
        // 
        this.AcceptButton = this.button1;
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.textBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
}

感谢任何建议。

0 个答案:

没有答案