Mình viết bắt sự kiện bàn phím khi nhấn phím nào thì phím đó đổi màu. Nhưng gặp chút vấn đề: Trong đoạn code mình viết khi click vào Button sau đó gõ phím tương ứng thì mới đổi màu button đó. Bạn nào sửa lại dùm mình là không cần click vào Button không? ( giống như máy chương trình tập gõ bàn phím)
Cám ơn nhiều!
Cám ơn nhiều!
Code:
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 WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button67_W(object sender, KeyEventArgs e) { if (e.KeyCode ==Keys.W) { button67.BackColor = Color.DarkOrange; } } private void button67_W2(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.W) { button67.BackColor = Color.MintCream; } } private void button1_Q(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Q) { button1.BackColor = Color.DarkOrange; } } private void button1_Q2(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Q) { button1.BackColor = Color.MintCream; } } } }
Code:
namespace WindowsFormsApplication1 { 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.button67 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button67 // this.button67.BackColor = System.Drawing.Color.MintCream; this.button67.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.button67.ForeColor = System.Drawing.Color.Navy; this.button67.Location = new System.Drawing.Point(154, 81); this.button67.Name = "button67"; this.button67.Size = new System.Drawing.Size(54, 48); this.button67.TabIndex = 75; this.button67.Text = "W"; this.button67.UseVisualStyleBackColor = false; this.button67.KeyDown += new System.Windows.Forms.KeyEventHandler(this.button67_W); this.button67.KeyUp += new System.Windows.Forms.KeyEventHandler(this.button67_W2); // // button1 // this.button1.BackColor = System.Drawing.Color.MintCream; this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.button1.ForeColor = System.Drawing.Color.Navy; this.button1.Location = new System.Drawing.Point(240, 81); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(54, 48); this.button1.TabIndex = 76; this.button1.Text = "Q"; this.button1.UseVisualStyleBackColor = false; this.button1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.button1_Q); this.button1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.button1_Q2); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(380, 218); this.Controls.Add(this.button1); this.Controls.Add(this.button67); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button67; private System.Windows.Forms.Button button1; } }
Comment