Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / Windows Forms

Visual Application Launcher

Rate me:
Please Sign up or sign in to vote.
4.91/5 (37 votes)
23 Jan 2012CPOL23 min read 107.9K   3.7K   116  
A WinForms UI using WCF services, Entity Framework, repository data access, repository caching, Unit of Work, Dependency Injection, and every other buzz work you can think of!
namespace VAL
{
    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 System.ServiceModel;
    using VAL.Contracts;
    using VAL.Model;
    using VAL.Controls;
    using VAL.Properties;
    using VAL.Contracts.ServiceFaults;

    /// <summary>
    /// A form to allow the administrator to edit group types
    /// </summary>
    public partial class GroupTypeMaintenance : PersistedForm
    {
        private GroupType groupType;
        private ListViewItem selectedItem;

        #region Construct and Load

        /// <summary>
        /// Ctor
        /// </summary>
        public GroupTypeMaintenance(IAdministratorService service)
        {
            InitializeComponent();

            this.Service = service;
            LoadGroupTypes();
            SetFormEditMode(false);
        }

        private IAdministratorService Service { get; set; }

        #endregion

        private void LoadGroupTypes()
        {
            try
            {
                groupTypesList.BeginUpdate();

                this.groupTypesList.Items.Clear();

                var groupTypes = Service.GetGroupTypes();
                foreach (var groupType in groupTypes)
                {
                    if (groupType.Id > 0)
                        groupTypesList.Items.Add(CreateListViewItem(groupType));
                }
            }
            finally
            {
                groupTypesList.EndUpdate();
            }
        }

        private ListViewItem CreateListViewItem(GroupType item)
        {
            return new ListViewItem()
            {
                Text = item.Description,
                Tag = item
            };
        }

        private void SetFormEditMode(bool editing)
        {
            groupTypesList.Enabled = (!editing);
            this.detailsBox.Enabled = editing;
            this.applyButton.Enabled = editing;
            if (!editing)
            {
                this.groupType = null;
                this.selectedItem = null;
                this.descriptionText.Text = String.Empty;
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            if (!groupTypesList.Enabled)
            {
                SetFormEditMode(false);
            }
            else
            {
                this.Close();
            }
        }

        private void EditGroupType()
        {
            if (!this.TryGetListViewItem(this.groupTypesList, out selectedItem))
                return;

            this.groupType = (GroupType)selectedItem.Tag;

            // Binds the user GUI fields to those of our object
            BindObjectFields();

            SetFormEditMode(true);
        }

        private void BindObjectFields()
        {
            BindingHelper.BindField(this.descriptionText, "Text", groupType, "Description");
        }

        private void groupTypesList_DoubleClick(object sender, EventArgs e)
        {
            EditGroupType();
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            if (!groupType.IsValid)
            {
                Messaging.ShowError(groupType.ErrorMessage);
                return;
            }

            try
            {
                groupType = Service.SaveGroupType(groupType);
            }
            catch (System.ServiceModel.FaultException<VAL.Contracts.ServiceFaults.BusinessRulesFault> ex)
            {
                Messaging.ShowError(ex.Message);
                return;
            }  
                        
            if (this.selectedItem == null)
            {
                ListViewItem lvi = CreateListViewItem(groupType); 
                this.groupTypesList.Items.Add(lvi);
            }
            else
            {
                this.selectedItem.Text = groupType.Description;
            }
                
            SetFormEditMode(false);
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.groupType = new GroupType();
            BindObjectFields();
            SetFormEditMode(true);
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditGroupType();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!this.TryGetListViewItem(this.groupTypesList, out selectedItem))
                return;

            GroupType gt = (GroupType)selectedItem.Tag;

            try
            {
                Service.DeleteGroupType(gt.Id);
            }
            catch (FaultException<BusinessRulesFault> ex)
            {
                Messaging.ShowError(ex.Message);
                return;
            }

            this.groupTypesList.Items.Remove(selectedItem);
            selectedItem = null;
        }

    }
}

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
Technical Lead
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions