TextBox的KeyDown事件没有触发,TextBox所在Form设置了AcceptButton, 敲‘回车’键,

changjiangyang 2018-03-16 09:32:05
用VS2015新建一个Winform项目,Form里添加一个Button 和一个TextBox,设置Button的Click事件和TextBox的keyDown事件,把Form的AcceptButton设置成刚才添加的Button,运行程序,将光标定位在TextBox里, 键盘上敲‘回车’键,Button的Click事件触发了,但是TextBox的KeyDown事件没有触发,为什么呢,在线急等高手。我把代码贴出来。
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProcessCharCMD
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


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:
namespace ProcessCharCMD
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <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;
}
}

...全文
840 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
IgoAheadNow 2018-11-30
  • 打赏
  • 举报
回复
这是一个事件处理先后的问题,优先响应Form的事件处理,然后才分派至控件进行事件触发。

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧