Click here to Skip to main content
12,503,238 members (61,117 online)
Click here to Skip to main content


165 bookmarked

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.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TimeApp.DataAccess;

namespace TimeApp.UI {

	public partial class MainForm : Form {

		// Fields.
		private ToolStripMenuItem miStart;
		private ToolStripMenuItem miPause;
		private ToolStripMenuItem miStop;
		private bool _projectsExist = false;
		private bool IsTracking = false;

		public MainForm() {
			niAppNotifyIcon.Visible = true;

		#region Build Menu Items

		/// <summary>
		/// Builds the dynamic menus for the application.
		/// </summary>
		private void BuildDynamicMenus() {
			// Create the menu items and add them to the list, if they are not already.
			if (miStart == null) {
				// Create the primary menu items.
				miStart = new ToolStripMenuItem("&Start", null, null, "miStart");
				miPause = new ToolStripMenuItem("&Pause", null, null, "miPause");
				miStop = new ToolStripMenuItem("Sto&p", null, null, "miStop");
				// Add the primary items to the list.
				List<ToolStripMenuItem> TimeEntryItems = new List<ToolStripMenuItem>();
				TimeEntryItems.AddRange(new ToolStripMenuItem[] { miStart, miPause, miStop });

				// Find the position on the notify icon menu strip to insert these items.
				int ItemPos = msActions.Items.IndexOf(miSeparatorBeforeTimeEntry) + 1;
				for (int i = TimeEntryItems.Count - 1; i >= 0; i--)
					msActions.Items.Insert(ItemPos, TimeEntryItems[i]);

			// Add the project menu items to the Start item.

		/// <summary>
		/// Builds a list of menu items for active projects.
		/// </summary>
		/// <returns></returns>
		private void BuildProjectItems(params ToolStripMenuItem[] menuItemsToBuildOn) {
			// Get the project list from the data source.
			List<ProjectSummary> Projects;
			using (TimeAppDataContext DataContext = new TimeAppDataContext())
				Projects = DataContext.GetAllProjectsAndTasks();

			_projectsExist = (Projects.Count > 0);
			if (_projectsExist) {
				// Add the child menu items for the projects to each menu item specified.
				foreach (ToolStripMenuItem MenuItem in menuItemsToBuildOn) {
					ToolStripMenuItem OtherMenuItem = null;

					// Create a list of the items for projects.
					List<ToolStripItem> Items = new List<ToolStripItem>();
					foreach (ProjectSummary ProjectItem in Projects) {
						// Project item.
						ToolStripMenuItem Item = new ToolStripMenuItem();
						Item.Text = ProjectItem.Name;
						Item.Tag = ProjectItem;

						// Add any task items.
						foreach (TaskSummary TaskItem in ProjectItem.Tasks) {
							// Task item.
							ToolStripMenuItem ChildItem = new ToolStripMenuItem();
							ChildItem.Text = TaskItem.Name;
							ChildItem.Tag = TaskItem;

						// Finish with a separator and "no task" item.
						if (ProjectItem.Tasks.Count > 0)
							Item.DropDownItems.Add(new ToolStripSeparator());
						OtherMenuItem = new ToolStripMenuItem("No task yet");
						OtherMenuItem.Tag = "T0";

					// Finish with a separator and "no project" item.
					Items.Add(new ToolStripSeparator());
					OtherMenuItem = new ToolStripMenuItem("No project yet");
					OtherMenuItem.Tag = "P0";

					// Add the list to the specified menu item.


		#region Main Menu Item Clicks

		private void miClose_Click(object sender, EventArgs e) {

		private void miAbout_Click(object sender, EventArgs e) {
			// Show the about dialog.
			using (AboutForm frmAbout = new AboutForm())

		private void miProjectsTasks_Click(object sender, EventArgs e) {
			// Show the project list dialog.
			using (ProjectListForm frmProjectList = new ProjectListForm())

			// Rebuild the dynamic menus.


		#region Notify Icon Item Clicks

		private void niAppNotifyIcon_DoubleClick(object sender, EventArgs e) {

		private void miShowTimeApp_Click(object sender, EventArgs e) {

		private void miStart_Click(object sender, EventArgs e) {
			// Get the project information from the menu item clicked.
			ToolStripItem MenuItem = sender as ToolStripMenuItem;
			if (MenuItem != null) {
				// Start a new log with the acquired information.
				IsTracking = true;

		private void miExit_Click(object sender, EventArgs e) {


		/// <summary>
		/// Shows and hides appropriate menu items based on whether or not a log entry is currently tracking.
		/// </summary>
		/// <param name="tracking">Indicates whether or not a log entry is currently tracking time.</param>
		private void ShowMenuItems() {
			if (_projectsExist) {
				miSeparatorBeforeExit.Visible = true;
				miStart.Visible = !IsTracking;
				miPause.Visible = IsTracking;
				miStop.Visible = IsTracking;
			} else {
				miSeparatorBeforeExit.Visible = false;
				miStart.Visible = false;
				miPause.Visible = false;
				miStop.Visible = false;

		/// <summary>
		/// Shows the main window if it is hidden and gives it focus.
		/// </summary>
		private void ShowWindow() {
			// Show the form again, if it is hidden.
			if (!Visible) {
				WindowState = FormWindowState.Normal;

			// Activate and focus this form.

		private void MainForm_Load(object sender, EventArgs e) {


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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).

You may also be interested in...

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