Saturday, October 8, 2011

Windows Form DateTimePicker

This windows control is simple to implement and hard to use in your application until you learn a little about how it works.

Even though you set the format of the date display to Short Date, the date is saved as date plus time. Time is stored in hours, minutes and seconds plus ticks and some other time span data that are not commonly used.

First problem I ran into was trying to set a new DateTime. I finally broke the DateTime down to month, date and year before doing some calculations on it. I then put it back together as new DateTime(m_year, m_month, m_day, 0, 0, 0)where the zeroes(0) are hours, minutes, seconds.

This is my search code:

private void button1_Click(object sender, EventArgs e)

{

var m_sound = System.Media.SystemSounds.Beep;

m_sound.Play();

string m_date = this.LookFor_dateTimePicker.Value.ToShortDateString();

m_date = m_date + " 12:00:00 AM";

DateTime m_find = Convert.ToDateTime(m_date);

appointmentsTableAdapter.FillByDate(this.reminderDataSet.Appointments, m_find);

}

This is code to set the time to 12:00:00 AM

private void aptDate_DTPicker_ValueChanged(object sender, EventArgs e)
        {
            DateTime m_date = this.aptDate_DTPicker.Value;
            DateTime m_keep = this.aptDate_DTPicker.Value;
 
            int m_month = m_date.Month;
            int m_day = m_date.Day;
            int m_year = m_date.Year;
            // set the Datetime picker time to 12:00:00 AM
            DateTime m_keepNew = new DateTime(m_year, m_month, m_day, 0, 0, 0);
            this.aptDate_DTPicker.Value = m_keepNew;
 
            int m_dayOfWeek = (int)m_date.DayOfWeek;
 
            if (m_dayOfWeek == 1)
            {
               m_day = m_day - 3;
            }
            else
            {
                m_day = m_day - 1;
            }
            DateTime m_newdate = new DateTime(m_year, m_month, m_day,0,0,0);
 
            this.sendReminder_DTPicker.Value = m_newdate;
 
            buildMessage(this.sendToNAME_TextBox.Text, m_date.ToShortDateString(), 
		this.aptTime_DTPicker.Value.ToShortTimeString(), this.aptLength_ComboBox.Text);
        }

No comments:

Post a Comment