Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

LINQ Challenges and SQL Server Compact Edition

, 25 Mar 2008 CPOL
Overcoming challenges with LINQ to SQL and using LINQ with SQL Server Compact Edition.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimeApp.UI.UserControls {

	/// <summary>
	/// Represents a duration in hours and/or minutes.
	/// </summary>
	public partial class DurationControl : UserControl {

		#region Properties

		/// <summary>
		/// Gets or sets a flag indicating whether or not a null value is allowed.
		/// </summary>
		public bool AllowNull {
			get {
				return chkNull.Visible;
			}
			set {
				chkNull.Visible = value;
				if (!value)
					chkNull.Checked = false;
			}
		}

		/// <summary>
		/// Gets or sets the duration represented by this control.
		/// </summary>
		public int? Duration {
			get {
				if (AllowNull && chkNull.Checked)
					return null;
				else
					return TimeToDuration((int)nudHours.Value, (int)nudMinutes.Value);
			}
			set {
				if (!AllowNull && !value.HasValue)
					throw new ArgumentNullException("Duration", "Duration cannot be null when AllowNull is false.");

				chkNull.Checked = !value.HasValue;
				if (value.HasValue) {
					TimeSpan DurationTime = DurationToTimeSpan(value.Value);
					nudHours.Value = DurationTime.Hours;
					nudMinutes.Value = DurationTime.Minutes;
				}
			}
		}

		/// <summary>
		/// Gets or sets the hours for the duration represented by this control.
		/// </summary>
		public byte Hours {
			get { return Convert.ToByte(nudHours.Value); }
			set { nudHours.Value = value; }
		}

		/// <summary>
		/// Gets or sets the minutes for the duration represented by this control.
		/// </summary>
		public byte Minutes {
			get { return Convert.ToByte(nudMinutes.Value); }
			set { nudMinutes.Value = value; }
		}

		#endregion

		public DurationControl() {
			InitializeComponent();
		}

		#region Helper Methods

		/// <summary>
		/// Converts a duration of minutes to a TimeSpan
		/// </summary>
		/// <param name="duration">The duration, in minutes, to convert.</param>
		/// <returns>A TimeSpan representing the specified duration.</returns>
		public static TimeSpan DurationToTimeSpan(int duration) {
			TimeSpan TimeDuration = new TimeSpan(duration / 60, duration % 60, 0);
			return TimeDuration;
		}

		/// <summary>
		/// Converts the specified hours and minutes to a duration (minutes only).
		/// </summary>
		/// <param name="hours">The hours component of the time to convert.</param>
		/// <param name="minutes">The minutes component of the time to convert.</param>
		/// <returns></returns>
		public static int TimeToDuration(int hours, int minutes) {
			return hours * 60 + minutes;
		}

		#endregion

		private void chkNull_CheckedChanged(object sender, EventArgs e) {
			flpDuration.Enabled = !chkNull.Checked;
		}

	}

}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Matt Sollars
Web Developer
United States United States
I began programming on my Commodore 64 at around the age of 12. After migrating to DOS and then Windows, I decided to take on the Web. Several languages and platforms later, I have settled in with .NET nicely. I am currently the owner of a software consulting company and lead application developer for a learning-based technology consultation company.
 
The love of a finished application is usually at war with the desire to improve it as soon as it's released (they're never really finished).

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411022.1 | Last Updated 25 Mar 2008
Article Copyright 2007 by Matt Sollars
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid