DevComponents.DotNetBar.Command使用方法

如前文所说,DotNetBar为用户制作界面程序提供了很大的方便。同时其也提供了许多其他的工具,例如本文提到的DevComponents.DotNetBar.Command。

DevComponents.DotNetBar.Command的思想是将界面设计与相应函数的实现分离,降低系统内部的耦合性,同时可以提高相应函数的复用性。我们可以把Command对象绑定到一个系统控件上,在控件的触发一定动作时通过Command的响应函数来实现相应操作。

例如:

示例一

InitializeComponent函数

 

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响应函数

private void command1_Executed(object sender, EventArgs e)
{
    MessageBox.Show("Just Test");
} 
运行效果图:
image 
当另外一个按钮button2的响应函数与button1相同时,只要将command1再绑定到button2上即可,这样减少了代码的重复编写,提高代码的复用性。

示例二

我们同样可以在button的响应函数中显式调用command来执行所需程序,代码如下。

private void buttonX1_Click(object sender, EventArgs e)
{
    command1.Execute();
}

由于ButtonX类实现了ICommandSource接口,从而可以方便地绑定执行命令,同时可以在CommandParameter属性中绑定需要传入的参数,在command的响应函数中可以读取参数中绑定的数据进行更多的操作。

posted on 2010-08-15 22:34  谷底望月  阅读(5013)  评论(2编辑  收藏  举报

导航