Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / C#

.NET TimePicker

Rate me:
Please Sign up or sign in to vote.
4.20/5 (39 votes)
27 Jun 20075 min read 311.2K   9.9K   83  
A variation on the standard .NET date-time picker
// C# TimePicker Class v1.0
// by Louis-Philippe Carignan - 10 July 2003
using System;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel;

namespace MyTimePicker
{
	/// <summary>
	/// The TimePickerValueEditor is used to edit the Value property of the TimePicker
	/// control during design-mode.  It is mainly used to display a HourMinuteSelectorForm
	/// on the screen to let the developer choose the time he wants.
	/// </summary>
	public class TimePickerValueEditor : UITypeEditor
	{
		private IWindowsFormsEditorService m_oEditorService;

		/// <summary>
		/// The form that will be displayed when the user click on the Value property
		/// in design-mode.
		/// </summary>
		private HourMinuteSelectorForm m_oHourMinuteForm;

		/// <summary>
		/// Constructor that does nothing.
		/// </summary>
		public TimePickerValueEditor()
		{
			m_oEditorService = null;
		}

		/// <summary>
		/// EditValue is inherited from UITypeEditor, it is used to create, place and
		/// show the HourMinuteSelectorForm on the screen.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="provider"></param>
		/// <param name="value"></param>
		/// <returns>The TimeSpan selected by the user</returns>
		public override Object EditValue(ITypeDescriptorContext context, 
										 IServiceProvider provider, 
										 Object value) 
		{

			if (context != null &&
				context.Instance != null &&
				provider != null) 
			{

				m_oEditorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

				if (m_oEditorService != null) 
				{
					m_oHourMinuteForm = new HourMinuteSelectorForm();
					m_oHourMinuteForm.Size = new Size(346, 135);
					m_oHourMinuteForm.Location = CalculateLocation(m_oHourMinuteForm.Size);
					
					m_oHourMinuteForm.SelectedTime = (TimeSpan)value;
					
					m_oEditorService.ShowDialog(m_oHourMinuteForm);

					value = m_oHourMinuteForm.SelectedTime;
				}
			}

			return value;
		}

		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) 
		{
			if (context != null && context.Instance != null) 
			{
				return UITypeEditorEditStyle.DropDown;
			}
			return base.GetEditStyle(context);
		}

		/// <summary>
		/// Private method to place a form inside the screen.  This will prevent
		/// the form of going outside the limits of the screen.
		/// </summary>
		/// <param name="sSizeOfForm">The size of the form</param>
		/// <returns>A point where the form should be place to stay in the screen</returns>
		private Point CalculateLocation(Size sSizeOfForm)
		{
			Point l_sPoint = Control.MousePosition;
		
			if (l_sPoint.Y + sSizeOfForm.Height > Screen.PrimaryScreen.WorkingArea.Height)
			{
				// We are near the bottom of the screen.  If the form appears, it won't
				// be entirely shown on the screen.  So place the form above the 
				// Time Picker
				l_sPoint = new Point(l_sPoint.X, l_sPoint.Y - 16 - sSizeOfForm.Height);
			}

			if (l_sPoint.X < 0)
			{
				// We are to much on the left side of the screen.  This causes the 
				// form to be outside the screen.  We have to push back the form 
				// on the edge of the screen.
				l_sPoint = new Point(0, l_sPoint.Y);
			}
			else if (l_sPoint.X + sSizeOfForm.Width > Screen.PrimaryScreen.WorkingArea.Width)
			{
				// This is the opposite of the previous statement.  We are now 
				// on the right side of the screen.  We have to stick the window to
				// the edge of the right border of the screen.
				l_sPoint = new Point(Screen.PrimaryScreen.WorkingArea.Width - sSizeOfForm.Width, l_sPoint.Y);
			}

			return l_sPoint;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
I'm a software engineer with 5 years of experience. I've been writting code mostly in .NET. My fields of expertise revolves around Windows Forms, Remoting, Xml, database connectivity, CompactFramework and GIS applications.

I'm an early adopter of Test Driven Developpement and Continuous Integration. I worked with tools such as Test Driven .NET, Cruise Control .NET, NAnt, NUnit and NCover. I am involved with the Agile user group in my city. My main area of interest is with lean software development but I do value other methodologies as well.

You can read my blog about lean software. It's mostly in French but I do have a few posts in English.
http://blogs.agilequebec.ca/blogs/lpcarignan/default.aspx

I'm always open to job offers. I am open to senior software engineer or achitect positions. If you are looking for someone with my profile, send me an e-mail. I am willing to relocate.

Comments and Discussions