Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); This. button1. Enter + =Button_enter; This. button1. Leave + =Button_leave; This. button2. Enter + =Button_enter; This. button2. Leave + =Button_leave; //continue to write the other } voidButton_leave (Objectsender, EventArgs e) {(Sender asButton). BackColor =SystemColors.Control; } voidButton_enter (Objectsender, EventArgs e) {(Sender asButton). BackColor =color.red; } }
//Subscribe to the same Click events for each buttonButton1. Click + =Button_click;button2. Click+=Button_click;button3. Click+=Button_Click;Private voidButton_Click (Objectsender, EventArgs e) {button[] Btnarr={button1, button2,button3}; Button btn= Sender asButton;//Current ButtonBtn. BackgroundImage = RESOURCE1.BACKIMG1;//Set the current button background to Backimg1,resource1 is the resource file class name//all the rest except the current button, all set another backgroundArray.foreach (btnarr.where (p = p = btn). ToArray (), p = = P.backgroundimage =resource1.backimg2);}
Button MouseEvent Color Change