Click here to Skip to main content
15,886,077 members
Articles / Desktop Programming / Windows Forms

Task Manager

Rate me:
Please Sign up or sign in to vote.
4.90/5 (105 votes)
1 Jan 2010CPOL4 min read 203.9K   21K   229  
Manage your daily tasks and To-Do list using some exciting features of Windows 7.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TaskManager.Database;
using TaskManager.Common;

namespace TaskManager
{
    public partial class AddCategoryForm : Form
    {
       
        #region Data members

        private int _categoryID = 0;
 TaskManagerEntities _taskMgrEntity = new TaskManagerEntities();

        #endregion

        #region Properties

        /// <summary>
        /// get category id
        /// </summary>
        public int CategoryID
        {
            get { return _categoryID; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
         public AddCategoryForm()
            : this(0)
        {

        }


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="categoryID">category id</param>
         public AddCategoryForm(int categoryID)
        {
            InitializeComponent();

            _categoryID = categoryID;
            btnDelete.Visible = (categoryID > 0);


            if (categoryID > 0)
                LoadCategory();
        }


        #endregion
        
        #region Private Methods

        /// <summary>
        /// Method to validate form data
        /// </summary>
        /// <returns>true, if all data is valid</returns>
        private bool ValidateForm()
        {
            bool res = true;
            #region Validate name

            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                errorProvider1.SetError(txtName, "Please provide category name.");
                res = false;
            }
            else
            {
                errorProvider1.SetError(txtName, string.Empty);
            }

            #endregion



            return res;
        }

        /// <summary>
        /// method to load category
        /// </summary>
        private void LoadCategory()
        {

            Category category = GetCategory(CategoryID);

            if (category != null)
            {
                txtName.Text = category.Name;
                txtDesciption.PlainText = category.Description;
                
            }

        }

        /// <summary>
        /// save/update category
        /// </summary>
        private void SaveCategory()
        {

            Category  category = null;
            bool isNew = false;

            if (CategoryID  > 0)
            {
                category = GetCategory(CategoryID);
            }

            if (category == null)
            {
                category = new Category();
                isNew = true;
            }

            category.Name = txtName.Text.Trim();
            category.Description = txtDesciption.PlainText;
            

            if (isNew)
            {
                _taskMgrEntity.AddToCategory(category);

            }

            _taskMgrEntity.SaveChanges();

        }

        /// <summary>
        /// get category by id
        /// </summary>
        /// <param name="categoryID">category id</param>
        /// <returns>category</returns>
        private Category GetCategory(int categoryID)
        {
            var matchs = from res in _taskMgrEntity.Category
                         where res.CategoryID == categoryID
                         select res;

            Category category = matchs.First();

            return category;
        }

        #endregion
        
        #region Events

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateForm())
            {

                Utility.ShowErrorMessage("Please provide valid information.", "Error to save resource.");
                return;
            }


            SaveCategory();

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dlgResult = Utility.ShowConfirmationBox("Are you sure, you want to delete category.", "Confirmation");

            if (dlgResult == DialogResult.Yes)
            {
                Category  category = GetCategory(CategoryID);

                if (category != null)
                {
                    _taskMgrEntity.DeleteObject(category);
                    _taskMgrEntity.SaveChanges();

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }

            }

        }

        #endregion

    }
}

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)


Written By
Chief Technology Officer
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

My Linked-In Profile

Comments and Discussions