在 toolStrip 中增加DateTimePicker 控件,并解决两个日期型控件联动问题


在 toolStrip 中增加DateTimePicker 控件,并解决两个日期型控件联动问题

      DateTimePicker dtpStart, dtpEnd; //两个日期控件

      private void initializeControls()
      {
         try
         {
            #region 增加 日期


            dtpStart = new DateTimePicker();
            //dtpStart.Format = DateTimePickerFormat.Custom;//自动设置
            //dtpStart.CustomFormat = "MM月dd日";//自定义格式 
            dtpStart.Format = DateTimePickerFormat.Short;
            dtpStart.Width = 120;
            dtpStart.Value = DateTime.Now;
            dtpStart.ShowCheckBox = true;
           // Controls.Add(dtpStart);
            ToolStripControlHost dateStartItem = new ToolStripControlHost(dtpStart);
            this.toolStrip1.Items.Insert(1, dateStartItem);

            dtpEnd = new DateTimePicker();
            //dtpEnd.Format = DateTimePickerFormat.Custom;//自动设置
            //dtpEnd.CustomFormat = "MM月dd日";//自定义格式 
            dtpEnd.Format = DateTimePickerFormat.Short;
            dtpEnd.Width = 120;
            dtpEnd.Value = DateTime.Now;
            dtpEnd.ShowCheckBox = true;
            //Controls.Add(dtpEnd);
            ToolStripControlHost dateEndItem = new ToolStripControlHost(dtpEnd);
            this.toolStrip1.Items.Insert(3, dateEndItem);

            dtpEnd.Enabled = false; //使之不能使用,以便控件两个控件联动.
            dtpStart.Checked = false;
            dtpEnd.Checked = false;
            #endregion

            //先注册声明一个事件绑定:
            dtpStart.Leave += new EventHandler(dtpStart_Leave);
            dtpStart.MouseLeave += new EventHandler(dtpStart_Leave);



            tsmCDSave.Enabled = false ;
            tsmHS.Enabled = false ;
            tsmHSsave.Enabled = false;
            tsmCF.Enabled = false;
            tsmCFsave.Enabled = false;
            tsmDJ.Enabled = false;
            tsmDJsave.Enabled = false;
            tsmTzUpt.Enabled = false;


         }
         catch (Exception exc)
         {
            MessageBox.Show(exc.Message, "异常信息 initializeControls ", MessageBoxButtons.OK, MessageBoxIcon.Error);

         }
      }

      private void dtpStart_Leave(object sender, EventArgs e)
      {
         try
         {
            if (dtpStart.Checked )
            {
               dtpEnd.Checked = true;
               dtpStart.Checked = true;
               dtpEnd.Enabled = true;
            }
            else if( !dtpStart.Checked  )
            {
               dtpEnd.Checked = false;
               dtpStart.Checked = false;
               dtpEnd.Enabled = false;
            }
            toolTip1.SetToolTip(this.dtpStart, "日期只能都选或者都不选");
         }
         catch (Exception exc)
         {
            MessageBox.Show(exc.Message, "异常信息 dtpStart_MouseEnter ", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }

      }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值