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.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() {
			InitializeComponent();
			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.
			BuildProjectItems(miStart);
		}

		/// <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;
						Items.Add(Item);

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

						// 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";
						Item.DropDownItems.Add(OtherMenuItem);
					}

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

					// Add the list to the specified menu item.
					MenuItem.DropDownItems.Clear();
					MenuItem.DropDownItems.AddRange(Items.ToArray());
				}
			}
		}

		#endregion

		#region Main Menu Item Clicks

		private void miClose_Click(object sender, EventArgs e) {
			Close();
		}

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

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

			// Rebuild the dynamic menus.
			BuildDynamicMenus();
			ShowMenuItems();
		}

		#endregion

		#region Notify Icon Item Clicks

		private void niAppNotifyIcon_DoubleClick(object sender, EventArgs e) {
			ShowWindow();
		}

		private void miShowTimeApp_Click(object sender, EventArgs e) {
			ShowWindow();
		}

		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;
				ShowMenuItems();
			}
		}

		private void miExit_Click(object sender, EventArgs e) {
			Application.Exit();
		}

		#endregion

		/// <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) {
				Show();
				WindowState = FormWindowState.Normal;
			}

			// Activate and focus this form.
			Activate();
			Focus();
		}

		private void MainForm_Load(object sender, EventArgs e) {
			BuildDynamicMenus();
			ShowMenuItems();
		}
		
	}

}

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
Web04 | 2.8.141223.1 | Last Updated 25 Mar 2008
Article Copyright 2007 by Matt Sollars
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid