使用另一个事件处理程序重新运行事件处理程序 [英] Rerun an event handler using another event handler

查看:108
本文介绍了使用另一个事件处理程序重新运行事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Windows Mobile 6智能手机编写了一个简单程序.

我使用DateTimePicker选择日期.然后,我使用标签显示日期-20天.如果选中了CheckBox,我希望标签显示日期-10天.我的问题是,如果我选中此复选框,除非重新输入日期,否则标签不会更新.

我在DTP事件处理程序中使用编写了代码,并认为最简单的方法是从CheckBox Eventhandler中重新运行该代码.我当然愿意接受其他任何建议.

I writing a simple program for a windows mobile 6 smartphone.

I use a DateTimePicker to select a date. I then use a label to show the date -20 days. If a CheckBox is checked, I want the label to show the date -10 days. My problem is that if I check the checkbox, the label doesn''t update unless I re-enter the date.

I wrote my code with-in the DTP event handler and think the easiest way is to re-run that code from the CheckBox Eventhandler. I am of course open to any other suggestions.

<pre lang="cs">
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyFirstProject
{
    public partial class MyFirstProject : Form
    {
        public MyFirstProject()
        {
            InitializeComponent();
        }
         void ckBox_CheckStateChanged(object sender, EventArgs e)
        {
//Would like to add code here to re-run code below, if that will work?
        }
            private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
            {

                    if (ckBox.Checked)
                        label20Day.Text = dateTimePicker1.Value.AddDays(-20).ToString("MM/dd/yyyy");
                    else label20Day.Text = dateTimePicker1.Value.AddDays(-10).ToString("MM/dd/yyyy");

            }
        }
}




我只使用MatLab编写了技术资料,这是我第一次尝试使用Compact Framework和C#.很抱歉,如果这不是一个新手,但我才刚开始!




I''ve only programmed technical stuff using MatLab and this is my first try using Compact Framework and C#. Sorry if this is too novice of a question but i''m just starting out!

推荐答案

这可以做到.

This would do it.

public partial class MyFirstProject : Form {
  int daysToShow;

  public MyFirstProject() {
    InitializeComponent();
    UpdatePeriod();
  }

  void ckBox_CheckStateChanged(object sender, EventArgs e) {
    UpdatePeriod();
    UpdateLabel();
  }

  private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
    UpdateLabel();
  }

  private void UpdatePeriod() {
    daysToShow = ckBox.Checked ? -20 : -10;
  }

  private void UpdateLabel() {
    label20Day.Text = dateTimePicker1.Value.AddDays(daysToShow).ToString("MM/dd/yyyy");
  }
}


方法是添加两个事件处理程序都调用的"updateDate"方法.
The way to do this is add an ''updateDate'' method that both event handlers call.


感谢双方的答案.我不敢相信在不到一个小时的时间内就提供了如此出色的答案.

我已经将该站点添加了书签,希望有一天能够为社区做出贡献.

我将继续编写这个小程序,并且会回来尝试继续学习我所能学到的更多知识.

干杯.

BC
Thanks for both of the answers. I can''t believe such a great answer was provided in less than an hour.

I''ve already bookmarked the site and hopefully someday will be able to contribute to the community.

I''m going to continue to write this little program and will be back trying to continue to learn as much as I can.

Cheers.

Bc


这篇关于使用另一个事件处理程序重新运行事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆