winform- 给自定义控件添加事件


下面是用户控件的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace winform_test.UserControls
{
    public partial class conYMPC : UserControl
    {
        private string sb_ym;

        public string Sb_ym
        {
            get { return sb_ym; }
        }
        private string sb_pc;

        public string Sb_pc
        {
            get { return sb_pc; }
        }

        //委托
        public delegate void MyChangeEventHandler();
        //事件
        public event MyChangeEventHandler MyChange;
        //方法
        public void OnMyChange()
        {
            if (MyChange != null)
                MyChange();
        }


        public conYMPC()
        {
            InitializeComponent();
        }
        /// 
        ///窗体初始化
        /// 
        /// 
        /// 
        private void conYM_Load(object sender, EventArgs e)
        {
            BindYear();
        }

        /// 
        /// 得到年份
        /// 
        private void BindYear()
        {
            string sql = "select distinct left(sb_ym,4)+'年' as sb_year from sb_ympc order by sb_year desc";
            DataSet ds = SqlHelp.GetDataSet(sql);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                cboY.Properties.Items.Add(dr["sb_year"].ToString());
            }
            cboY.SelectedIndex = 0;
        }
        /// 
        /// 得到月份
        /// 
        private void BindMonth()
        {
            cboM.Properties.Items.Clear();
            string sql = "select distinct right(sb_ym,2)+'月' as sb_month from sb_ympc where sb_ym like '" + cboY.Text.Substring(0, 4) + "%' order by sb_month desc";
            DataSet ds = SqlHelp.GetDataSet(sql);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                cboM.Properties.Items.Add(dr["sb_month"].ToString());
            }
            cboM.SelectedIndex = 0;
        }
        /// 
        /// 得到批次
        /// 
        private void BindPC()
        {
            cboPC.Properties.Items.Clear();
            string sql = "select sb_pc from sb_ympc where sb_ym='" + cboY.Text.Substring(0, 4) + cboM.Text.Substring(0, 2) + "' order by sb_pc";
            DataSet ds = SqlHelp.GetDataSet(sql);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                cboPC.Properties.Items.Add(dr["sb_pc"].ToString());
            }
            cboPC.SelectedIndex = 0;
        }
        /// 
        /// 年份下拉列表框改变事件
        /// 
        /// 
        /// 
        private void cboY_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindMonth();
            CheckChange();
        }
        /// 
        /// 月份下拉列表框改变事件
        /// 
        /// 
        /// 
        private void cboM_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPC();
            CheckChange();
        }
        /// 
        /// 批次下拉列表框改变事件
        /// 
        /// 
        /// 
        private void cboPC_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckChange();
        }

        /// 
        /// 检查下拉列表框的值是否改变,改变的话更新
        /// 
        private void CheckChange()
        {
            string _sb_ym = cboY.Text.Substring(0, 4) + cboM.Text.Substring(0, 2);
            string _sb_pc = cboPC.Text;
            if (_sb_ym != sb_ym || _sb_pc != sb_pc)
            {
                sb_ym = _sb_ym;
                sb_pc = _sb_pc;
                OnMyChange();//触发这个方法
            }
        }
    }
}

这个图片是用户控件的截图:

下面是调用用户控件窗体的代码:

private void frmStart_Load(object sender, EventArgs e)
        {
            //给控件绑定事件
            conYMPC1.MyChange += new UserControls.conYMPC.MyChangeEventHandler(aa);
        }

        void aa()
        {
            MessageBox.Show("申报年月:" + conYMPC1.Sb_ym + "  申报批次:" + conYMPC1.Sb_pc);
        }

相关