In the construction of WinForm program, in order to make the structure of the system clear, there is a good user interaction experience, to achieve the interaction between the different buttons, do not make the main form inside the code bloated. It is necessary to implement command management by binding the command of a button to a class.
This article is binding on how to Implement button events for buttons.
1. New WinForm Program
2. Add 3 button buttons as shown
3, the new ICommand.cs interface, the code is as follows
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacecommandtest{//Command Interface Public Interface ICommand {voidOnClick (); voidNew (); voidRefresh (); }}
4, the new command management class CommandUnitily.cs, the code is as follows
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacecommandtest{class commandunitily {//Create a command collection Private Staticlist<ICommand> Cmds =Newlist<ICommand>(); //Add a command to the collection Public Static voidcmdregist (ICommand cmd) {Cmds. ADD (CMD); } //refreshes all commands for direct interaction with commands Public Static voidAllrefresh () {foreach(ICommandCmdinchCmds) {cmd. Refresh (); } } }}
5, the New button buttons General class ButtonTool.cs, the code is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacecommandtest{//Button control common class, inherited from ICommand interface class buttontool : ICommand {Private Button bt; voidBt_click (Objectsender, EventArgs e) {OnClick (); commandunitily. Allrefresh ();//all buttons are refreshed, including the button itself } //After using the virtual keyword, after calling the OnClick method,//the onclick in the derived class (example: Button1Click) is called,//to achieve their own button click events Public Virtual voidOnClick () { Console. WriteLine ("Click the button event"); } Public voidNew (Button But) {BT=But ; Bt. Click+=New EventHandler(Bt_click); } Public voidRefresh () { Console. WriteLine (BT. Name+"Refresh"); } Public voidNew () {Throw Newnotimplementedexception (); } }}
6, create Button1Click.cs Click Class, the code is as follows :
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacecommandtest{//Create Button1 event execution class, inherited from Buttontool class button1click : buttontool { PublicButton1Click (Button obj) { Console. WriteLine ("Button1 Registration Successful"); This. New (obj); } //override the OnClick method of the Buttontool class to implement Button1 click events Public Override voidOnClick () {Base. OnClick (); Console. WriteLine ("Click the Button1 event"); } } }
7. The main form Form.cs code is as follows :
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacecommandtest{ Public Partial class Form1 : Form { PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) { commandunitily. Cmdregist (NewButton1Click (button1));
Console. WriteLine ("form loaded complete"); } }}
8. The file structure is as follows:
9. Testing
Load from form to click Button1 test results such as:
You can use the Button1Click.cs class to implement Button2 and Button3 button click events
Source code: Http://pan.baidu.com/s/1eSwoh5C
C # Command Binding