header
CodePanic! > C#.NET Tips > 今ここ

■カレンダーの使い方

MonthCalendarをFormに貼り付けるだけで
月のカレンダーを簡単に表示することができます。

例では当日の日付が□で囲まれています(デフォルトで囲まれます)
さらに日付を選ぶと(マウスドラッグで範囲選択可能)
その開始日と終了日をラベルに表示しています。
monthcalendar.jpg(43985 byte)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 日付が選択された場合に発生するイベントを設定
            monthCalendar1.DateSelected += new DateRangeEventHandler(monthCalendar1_DateSelected);
        }

        void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            // 開始〜終了を表示
            label1.Text = e.Start.ToString() + " 〜 " + e.End.ToString();
        }
    }
}





Copyright © 2008.07 - shougo suzaki