• C# 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();
            }
            /// <summary>
            ///窗体初始化
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void conYM_Load(object sender, EventArgs e)
            {
                BindYear();
            }
    
            /// <summary>
            /// 得到年份
            /// </summary>
            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;
            }
            /// <summary>
            /// 得到月份
            /// </summary>
            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;
            }
            /// <summary>
            /// 得到批次
            /// </summary>
            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;
            }
            /// <summary>
            /// 年份下拉列表框改变事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void cboY_SelectedIndexChanged(object sender, EventArgs e)
            {
                BindMonth();
                CheckChange();
            }
            /// <summary>
            /// 月份下拉列表框改变事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void cboM_SelectedIndexChanged(object sender, EventArgs e)
            {
                BindPC();
                CheckChange();
            }
            /// <summary>
            /// 批次下拉列表框改变事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void cboPC_SelectedIndexChanged(object sender, EventArgs e)
            {
                CheckChange();
            }
    
            /// <summary>
            /// 检查下拉列表框的值是否改变,改变的话更新
            /// </summary>
            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);
            }
    复制代码

    这样在改变用户控件中的值的时候,在窗体终究可以监听到这个事件什么时候触发。

  • 相关阅读:
    PHP 数据库连接
    php函数
    php数组基础
    Jquery元素追加和删除
    jquery获取父,同,子级元素
    深入理解css中position属性及z-index属性
    php运算符
    MySQL主键与索引的区别和联系
    php 常量
    20150408--Sphinx+邮件激活-02
  • 原文地址:https://www.cnblogs.com/as3lib/p/6414723.html
Copyright © 2020-2023  润新知