Click here to Skip to main content
15,886,857 members
Articles / Database Development / SQL Server / SQL Server 2008

Architecture Guide: Windows Forms, Generics, Auto-Mapper, Entity Framework, Framework Design, and many more..

Rate me:
Please Sign up or sign in to vote.
4.93/5 (39 votes)
12 Dec 2013CPOL15 min read 141.3K   3.8K   175  
Architecting a Windows Forms based system on top of a framework. This will help you develop a form based application faster.
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;
using Demo.Core;
using Demo.Core.Models;
using Demo.Common;
using Demo.Customer.Models;
using Demo.Customer.Handlers;

namespace Demo.Customer.ChildControls
{
    public partial class TrackingPoint : UserControl
    {
        public enum TrackingPointModes
        {
            LastItem,
            RegularItem,
            FullView,
            SmallView
        }
        private HandlerTrackingPoint handlerTracking = new HandlerTrackingPoint();
        private HandlerReaderPacket handlerPacket = new HandlerReaderPacket();
        private HandlerWeightingView handlerWeighting = new HandlerWeightingView();

        private DateTime lastPacketUpdatedTime = DateTime.Now;
        private DateTime lastWeightUpdatedTime = DateTime.Now;
        private bool editFlag = true;
        public delegate void AddNewTrackingPoint(TrackingPoint sender);
        public event AddNewTrackingPoint OnAddNewTrackingPoint;

        public delegate void RemoveTrackingPoint(TrackingPoint sender);
        public event RemoveTrackingPoint OnRemoveTrackingPoint;

        public delegate void PacketUpdated(List<string> packetData);
        public event PacketUpdated OnPacketUpdated;
       
        private TrackingPointModes _trackingPointMode;
        public TrackingPointModes TrackingPointMode
        {
            get { return _trackingPointMode; }
            set
            {
                _trackingPointMode = value;
                switch (_trackingPointMode)
                {
                    case TrackingPointModes.LastItem:
                        {
                            this.pictureBoxArrow.Visible = false;
                            this.Size = new Size(150, this.Height);
                        } break;
                    case TrackingPointModes.FullView: { } break;
                    case TrackingPointModes.SmallView: { } break;
                }
            }
        }

        private bool _doTrackThisUnit = false;
        public bool DoTrackThisUnit
        {
            get { return _doTrackThisUnit; }
        }
        public string ReaderId
        {
            get { return this.buttonReader.Text; }
            set { this.buttonReader.Text = value; }
        }

        public int IntReaderId
        {
            get { return NullHandler.ConvertToInt(this.buttonReader.Text); }
        }

        public string TrackingPointText
        {
            get { return this.labelLocation.Text; }
            set { this.labelLocation.Text = value; }
        }

        private int _trackingPointId;
        public int TrackingPointId
        {
            get { return _trackingPointId; }
            set
            {
                _trackingPointId = value;
                _trackingView = handlerTracking
                    .GetSingle(_trackingPointId);
                if (_trackingView == null) return;
                this.labelLocation.Text = _trackingView.Name;
                this.buttonReader.Text = "" + _trackingView.ReaderId;
                this._doTrackThisUnit = _trackingView.DoRecordPacket;
                UpdateUI(_trackingView.DoRecordPacket);
            }
        }

        public TrackingPoint()
        {
            InitializeComponent();
        }

        private TrackingPointView _trackingView;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        private void pictureBoxArrow_DoubleClick(object sender, EventArgs e)
        {
            if (OnAddNewTrackingPoint != null)
                OnAddNewTrackingPoint(this);
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (OnRemoveTrackingPoint != null)
                OnRemoveTrackingPoint(this);
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (editFlag)
            {
                this.textBoxLocation.Visible = true;
                this.textBoxLocation.Text = this.labelLocation.Text;
                this.textBoxReaderId.Visible = true;
                this.textBoxReaderId.Text = this.buttonReader.Text;
                this.buttonEdit.Image = global::Demo.Customer.Properties.Resources.Save;
                editFlag = false;
            }
            else
            {
                this.textBoxLocation.Visible = false;
                this.labelLocation.Text = this.textBoxLocation.Text;
                this.textBoxReaderId.Visible = false;
                this.buttonReader.Text = this.textBoxReaderId.Text;
                this.buttonEdit.Image = global::Demo.Customer.Properties.Resources.Edit;
                editFlag = true;
                TrackingPointView trackingView = new TrackingPointView();
                trackingView = _trackingView;
                trackingView.Name = this.labelLocation.Text;
                trackingView.ReaderId = this.IntReaderId;
                handlerTracking.Update(trackingView);
            }
        }

        private void buttonMonitor_Click(object sender, EventArgs e)
        {
            UpdateUI(!_doTrackThisUnit);
            _trackingView.DoRecordPacket = !_doTrackThisUnit;
            _doTrackThisUnit = !_doTrackThisUnit;
            handlerTracking.Update(_trackingView);
        }

        private void UpdateUI(bool doRecordPacket)
        {
            if (!doRecordPacket)
            {
                this.buttonMonitor.BackColor = Color.PaleGreen;
                this.buttonPinging.Image = global::Demo.Customer.Properties.Resources.BlackBullet;
            }
            else
            {
                this.buttonMonitor.BackColor = Color.DarkGreen;
                this.buttonPinging.Image = global::Demo.Customer.Properties.Resources.YellowBullet;
            }
        }

        public void Remove(int readerId)
        {
            if (readerId == this.IntReaderId)
            {
                TrackingPointView trackingVIew = handlerTracking
                    .GetTrackingPointViewOptimized(this.IntReaderId);
                trackingVIew.DoRecordPacket = false;
                handlerTracking.Update(trackingVIew);
            }
        }

        public List<ReaderPacketView> TrackingData()
        {
            if (_doTrackThisUnit)
            {
                this.buttonPinging.Image = global::Demo.Customer.Properties.Resources.YellowBullet;
                List<ReaderPacketView> packets = handlerPacket.GetLatest(this.ReaderId, lastPacketUpdatedTime);
                lastPacketUpdatedTime = DateTime.Now;
                return packets;
            }
            return new List<ReaderPacketView>();
        }

        internal List<WeightingView> WeightingData()
        {
            if (_doTrackThisUnit)
            {
                this.buttonPinging.Image = global::Demo.Customer.Properties.Resources.YellowBullet;
                List<WeightingView> packets = handlerWeighting.GetLatest(this.TrackingPointId, lastWeightUpdatedTime);
                lastWeightUpdatedTime = DateTime.Now;
                return packets;
            }
            return new List<WeightingView>();
        }

        internal void Closing()
        {
            handlerPacket.RemoveAll();
        }
    }
}

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
Architect Virtusa Pvt. Ltd.
Sri Lanka Sri Lanka
In-depth coverage of Microsoft .Net, Cloud and many other cutting-edge Technologies.

- The Mandelbrot set – someone has called it the thumb-print of God – is one of the most beautiful and remarkable discoveries in the entire history of mathematics. My profile picture is generated with that equation.

You may contact Nirosh for Consultations, Code Reviews and Architecture Guide Workshops via c_nir*o*sh@hotmail.com (Remove * to use)



View Nirosh L.W.C.'s profile on LinkedIn


Other Links

Comments and Discussions