下面是用户控件的代码:
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); }
这样在改变用户控件中的值的时候,在窗体终究可以监听到这个事件什么时候触发。
-----------------------------
天王盖地虎小说网:http://www.twgdh.com/