Monday, August 21, 2006

Form DateCalculator

Sometimes I need to calculate date, this form can help
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DateCalculator
{
    public partial class FrmDateCalculator : Form
    {
        public FrmDateCalculator()
        {
            InitializeComponent();
        }

        private void btnIncrease_Click(object sender, EventArgs e)
        {
            txtResult.Text = dtpDate.Value.AddDays((double)nddDays.Value).ToShortDateString();
        }

        private void btnDecrease_Click(object sender, EventArgs e)
        {
            txtResult.Text = dtpDate.Value.AddDays(-(double)nddDays.Value).ToShortDateString();
        }

        private void btnTryFeb_Click(object sender, EventArgs e)
        {
            DateTime dtm = new DateTime(DateTime.Today.Year, 2, 30);
            txtResult.Text = dtm.ToShortDateString();
        }

        private void btnGetPrevMonday_Click(object sender, EventArgs e)
        {
            DateTime dtmStart = DateTime.Today;
            DateTime dtmValue = dtpDate.Value;
            int intDayOfWeek = Convert.ToInt32(dtmValue.DayOfWeek);

            if (intDayOfWeek == 0)
            {
                dtmValue = dtmValue.AddDays(-1);
                intDayOfWeek = 6;
            }
            dtmStart = dtmValue.AddDays(-((intDayOfWeek+6)-7));
            
            DateTime dtmEnd = dtmStart.AddDays(6);
            MessageBox.Show(dtmStart.ToString("dd/MM/yyyy") + " - " + dtmEnd.ToString("dd/MM/yyyy"));
        }

        private void btnMinValue_Click(object sender, EventArgs e)
        {
            DateTime dtm1 = DateTime.MinValue;
            DateTime dtm2 = new DateTime(1900, 1, 1);
            bool blnEqual = dtm1 == dtm2;
            MessageBox.Show(dtm1.ToString("dd/MM/yyyy") + " == " + dtm2.ToString("dd/MM/yyyy") + " -> " + blnEqual.ToString());
        }
    }
    
}
As you can see, this form already using "partial" keyword. With "partial", the definition of a class, struct or interface can be splitted into multiple files.

No comments:

Post a Comment