As mentioned above, dotnetbar provides great convenience for users to create interface programs. It also provides many other tools, such as devcomponents. dotnetbar. Command mentioned in this article.
The idea of devcomponents. dotnetbar. Command is to separate the interface design from the implementation of the corresponding functions to reduce coupling within the system and improve the reusability of the corresponding functions. We can bind the command object to a system control. When the control triggers a certain action, we can use the command response function to implement the corresponding operation.
For example:
Example 1
Initializecomponent Function
private void InitializeComponent()
{ this.buttonX1 = new DevComponents.DotNetBar.ButtonX();
this.command1 = new DevComponents.DotNetBar.Command();
this.SuspendLayout();
//
// buttonX1
//
this.buttonX1.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
this.buttonX1.ColorTable = DevComponents.DotNetBar.eButtonColor.OrangeWithBackground;
this.buttonX1.Command = this.command1;
this.buttonX1.Location = new System.Drawing.Point(367, 51);
this.buttonX1.Name = "buttonX1";
this.buttonX1.Text = "buttonX1";
//
// command1
//
this.command1.Executed += new System.EventHandler(this.command1_Executed);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(549, 363);
this.Controls.Add(this.buttonX1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
Command1 Response Function
private void command1_Executed(object sender, EventArgs e)
{ MessageBox.Show("Just Test");}
Run:
When the response function of another button button2 is the same as that of button1, you only need to bind command1 to button2. This reduces code duplication and improves code reusability.
Example 2We can also explicitly call command in the response function of the button to execute the required program. The Code is as follows.
private void buttonX1_Click(object sender, EventArgs e)
{ command1.Execute();
}
Because the buttonx class implements the icommandsource interface, you can easily bind execution commands and bind parameters to be passed in the commandparameter attribute, in the response function of the command, you can read the data bound to the parameter for more operations.