"""rslCalendarForm contains the code for creating a Windows Form with a Calendar control to let the user choose a Date value. Usage: execfile(dirname(__file__)+"\\rslCalendarForm.py") #use execfile with the full path to the file name to load the module frmCalendar = CalendarForm("Window Title") #Create and load the form. GUI thread is blocked until the form is closed dtSelected = CalendarForm.LastDateSelected #Retrieve selected value from the static CalendarForm.LastDateSelected variable. """ #NOTE: Form code taken from http://www.voidspace.org.uk/ironpython/presentation.html and was originally written by Michael Foord import sys import clr clr.AddReference('System.Drawing') clr.AddReference('System.Windows.Forms') from System.Windows.Forms import Application, Form from System.Windows.Forms import Button as frmButton from System.Windows.Forms import (ControlStyles, ContextMenuStrip, DockStyle, FormStartPosition, ImageList, Keys, MenuStrip, MessageBox, MessageBoxButtons, MessageBoxIcon, Panel, TabControl, TabAlignment, TabPage, ToolStrip, ToolStripButton, ToolStripMenuItem,ToolStripLabel, ToolStripItemDisplayStyle, WebBrowser, Padding, FormBorderStyle,SizeGripStyle) from System.Windows.Forms import (MonthCalendar, Day) from System.Drawing import Size,Point from System import Uri, Exception, DateTime class CalendarForm(Form): "Windows Form that displays a Calendar control. This will run in the main GUI thread." LastDateSelected = None #Static value used to store the last date selected def __init__(self, Name="Calendar",dtStartDate=None): "Name is the title of the control. Callback is a method that accepts a Datetime as a parameter and will be called when the form is closed." Form.__init__(self) CalendarForm.LastDateSelected = None #Clear last date selection self.Text = Name self.Name = Name self.Width = 210 self.Height = 230 self.FormBorderStyle = FormBorderStyle.FixedDialog self.SizeGripStyle = SizeGripStyle.Hide self.MinimizeBox = False self.MaximizeBox = False self.initMenu() self.initCalendar(dtStartDate) self.CenterToParent() self.ShowDialog() def createToolstripButton(self, name, text, clickHandler=None, keys=None): menuItem = ToolStripButton(Name = name, Text = text) if keys: menuItem.ShortcutKeys = keys if clickHandler: menuItem.Click += clickHandler return menuItem def initMenu(self): menuStrip = MenuStrip( Name = "Main MenuStrip", Dock = DockStyle.Bottom ) mSpacer = ToolStripLabel(" ") mBack = self.createToolstripButton('Save', '&Save', self.onSave) mSpacer2 = ToolStripLabel(" ") mForward = self.createToolstripButton('Cancel', '&Cancel', self.onCancel) menuStrip.Items.Add(mSpacer) menuStrip.Items.Add(mBack) menuStrip.Items.Add(mSpacer2) menuStrip.Items.Add(mForward) self.Controls.Add(menuStrip) def initCalendar(self, StartDate=None): "Setup the Calendar Control." pCalendar = Panel(Dock = DockStyle.Fill) pCalendar.Padding = Padding(25, 10, 25, 10) self.mCalendar = MonthCalendar() self.mCalendar.Location = Point (10, 10) self.mCalendar.FirstDayOfWeek = Day.Sunday self.mCalendar.ShowTodayCircle = False self.mCalendar.ShowToday = False self.mCalendar.ShowWeekNumbers = False self.mCalendar.MaxSelectionCount = 1 self.mCalendar.DoubleClick += self.onSave if StartDate==None: self.mCalendar.SetDate(DateTime.Now) else: self.mCalendar.SetDate(StartDate) pCalendar.Controls.Add(self.mCalendar) self.Controls.Add(pCalendar) def onSave(self, _, __): "Set the CalendarForm.LastDateSelected to the selected date and then close the form." CalendarForm.LastDateSelected = self.mCalendar.SelectionStart self.Close() def onCancel(self, _, __): self.Close()