委托使用,也是参考百度,给自己做个笔记。
委托自己理解就是定义一个接口,把接口公布出去,自己会调用接口。但接口具体怎么实现,是在别处实现。因为实现的时候通常要使用到实现类里面的参数。
这样就实现了两个页面之间的配合,既可以获取到委托订定义类UCBarcodeGridView调用页面的状态数据,又可以获取到实际执行页面的状态数据
UCBarcodeGridView 类是个自定义控件类,定义了委托,并在自己的方法里面调用委托类。
namespace ScreamWMS.UserControls
{
'声明委托
public delegate void ViewDefineBarcode(Boolean isConfir,DataTable tab);
public partial class UCBarcodeGridView : DevExpress.XtraEditors.XtraUserControl
{
'声明委托类变量
public event ViewDefineBarcode OnbtinViewDefineBarcodeClick;
private DataTable tab;
public UCBarcodeGridView(DataTable tab)
{
InitializeComponent();
this.tab = tab;
this.gridControl1.DataSource = tab;
private void btnCancel_Click(object sender, EventArgs e)
{
OnbtinViewDefineBarcodeClick(false, tab);
}
private void btnConfirm_Click(object sender, EventArgs e)
{
OnbtinViewDefineBarcodeClick(true, tab);
}
private void btnSearch_Click(object sender, EventArgs e)
{
string newFilter = "1=1 ";
string filter = gridView1.ActiveFilterString;
if (txtProductName.Text != string.Empty)
{
newFilter = newFilter + string.Format(" And productName like '%{0}%'", txtProductName.Text.Trim());
}
if (txtSpec.Text != string.Empty)
{
newFilter = newFilter + string.Format(" And Spec like '%{0}%'", txtSpec.Text.Trim());
}
this.gridView1.ActiveFilterString = newFilter;
}
private void txtFilter_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
string newFilter = "1=1 ";
string filter = gridView1.ActiveFilterString;
if (txtBrand.Text.Trim() != string.Empty)
{
newFilter = newFilter + string.Format(" And brandcode like '%{0}%'", txtBrand.Text.Trim());
}
if (txtProductName.Text.Trim() != string.Empty)
{
newFilter = newFilter + string.Format(" And productName like '%{0}%'", txtProductName.Text.Trim());
}
if (txtSpec.Text.Trim() != string.Empty)
{
newFilter = newFilter + string.Format(" And Spec like '%{0}%'", txtSpec.Text.Trim());
}
if (txtProductCode.Text.Trim() != string.Empty)
{
newFilter = newFilter + string.Format(" And ProductCode like '%{0}%'", txtProductCode.Text.Trim());
}
this.gridView1.ActiveFilterString = newFilter;
}
}
}
}
B类实例化了委托类变量
Class B{
private void btnViewDefineBarcode_Click(object sender, EventArgs e)
{
UCBarcodeGridView barcodeListTree = new UCBarcodeGridView(tab);
this.Controls.Add(barcodeListTree);
barcodeListTree.BringToFront();
barcodeListTree.OnbtinViewDefineBarcodeClick += new ViewDefineBarcode(delegate(Boolean isChoose, DataTable gbTable)
{
if (isChoose)
{
foreach (DataRow row in gbTable.Rows)
{
int barcodeCount = Convert.ToInt32(row["Qty"]);
if (barcodeCount != 0)
{
for (int i = 0; i < barcodeCount; i++)
{
this.txtBarcode.Text = row["ProductCode"].ToString().Trim();
txtBarcode_KeyPress(txtBarcode, new KeyPressEventArgs('\r'));
}
}
}
}
this.Controls.Remove(barcodeListTree);
});
barcodeListTree.Show();
}
}