委托使用

本文介绍了C#中的委托使用,通过一个自定义控件UCBarcodeGridView的示例,展示了如何定义委托、声明事件并进行事件处理。在UCBarcodeGridView中,定义了一个名为ViewDefineBarcode的委托,用于传递数据和状态。在类B中,实例化UCBarcodeGridView并订阅其事件,实现了不同页面间的交互和数据传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

委托使用,也是参考百度,给自己做个笔记。

委托自己理解就是定义一个接口,把接口公布出去,自己会调用接口。但接口具体怎么实现,是在别处实现。因为实现的时候通常要使用到实现类里面的参数。

这样就实现了两个页面之间的配合,既可以获取到委托订定义类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();

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值