做法是抓取所有的Button,並註冊這兩個事件:
public Form1() { InitializeComponent(); foreach (var control in this.Controls) { if (control.GetType() == typeof(Button)) { Button btn = (Button)control; btn.GotFocus += btn_GotFocus; btn.LostFocus += btn_LostFocus; } } } void btn_LostFocus(object sender, EventArgs e) { //改變該Button的樣式 Button btn = (Button)sender; btn.BackColor = SystemColors.Control; } void btn_GotFocus(object sender, EventArgs e) { //改變該Button的樣式 Button btn = (Button)sender; btn.BackColor = System.Drawing.Color.Yellow; //變為黃色 }
沒有留言:
張貼留言