使用另一个事件处理程序重新运行事件处理程序

编程入门 行业动态 更新时间:2024-10-27 20:30:54
本文介绍了使用另一个事件处理程序重新运行事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我为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

更多推荐

使用另一个事件处理程序重新运行事件处理程序

本文发布于:2023-11-09 15:01:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572672.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!