Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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");

            }
        }
}



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!
Posted

This would do it.

C#
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");
  }
}
 
Share this answer
 
The way to do this is add an 'updateDate' method that both event handlers call.
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900