C# winformTextBox 键盘监听
Step 1:添加监听
- 方式1:Designer中添加监听
this.textBox1.Location = new System.Drawing.Point(12, 5); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(548, 188); this.textBox1.TabIndex = 0; this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);//添加键盘事件
- 方式2:CS代码里添加监听
public Form1() { InitializeComponent(); textBox1.TextChanged += new EventHandler(textBox1_TextChanged); textBox1.KeyDown+=new KeyEventHandler(textBox1_KeyDown);//添加键盘事件 }
Step 2 : 监听事件
示例:
- 添加Ctrl+A 全选事件
/// <summary> /// 控件事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == Keys.A && e.Control) { textBox1.SelectAll(); } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。