Click here to Skip to main content
15,885,933 members
Articles / Mobile Apps / Windows Mobile

Task assigner with Windows Mobile and a Web Service

Rate me:
Please Sign up or sign in to vote.
4.23/5 (12 votes)
7 Dec 2008CPOL3 min read 54.9K   1.1K   35  
Send messages, commands, tasks etc., to all employees from a central administration point.
//------------------------------------------------------------------------------
/// <copyright from='1997' to='2005' company='Microsoft Corporation'>
///		Copyright (c) Microsoft Corporation. All Rights Reserved.
///
///   This source code is intended only as a supplement to Microsoft
///   Development Tools and/or on-line documentation.  See these other
///   materials for detailed information regarding Microsoft code samples.
/// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;

namespace FileTransfer
{
    sealed class FormCache : CollectionBase
    {
        private System.Collections.ArrayList stack = new ArrayList();
        public static readonly FormCache Instance = new FormCache();

        private FormCache()
        {
        }

        #region Properties
        private Form rootForm;
        public Form RootForm
        {
            get { return rootForm; }
            set { rootForm = value; }
        }

        private bool activation = false;
        public bool Activation
        {
            get { return activation; }
            //set { activation = value; }
        }
        #endregion

        #region Cache Management

        #region Load
        /// <summary>
        /// Create instance of form and add to cache.
        /// </summary>
        /// <param name="formType">Type of form to add to the cache</param>
        /// <returns>Created form</returns>
        public Form Load(Type formType)
        {
            // Check if in cache already, and if so, return instance
            foreach (Form f in List)
            {
                if (f.GetType().Name.Equals(formType.Name))
                {
                    return f;
                }
            }

            // Create new instance, translate, add to collection and return
            Form form = (Form)Activator.CreateInstance(formType);           
            List.Add(form);
            return form;
        }
        #endregion

        #region Dispose
        /// <summary>
        /// Dispose form in cache.
        /// </summary>
        /// <param name="formType">Type of form</param>
        public void DisposeForm(Type formType)
        {
            foreach (Form f in List)
                if (f.GetType().Name.Equals(formType.Name))
                {
                    f.Dispose();
                    List.Remove(f);
                    break;
                }
        }

        /// <summary>
        /// Dispose all forms and clear List.
        /// </summary>
        public void DisposeAllForms()
        {
            foreach (Form f in List)
            {
                f.Dispose();
            }
            List.Clear();
        }
        #endregion

        #endregion

        #region Stack Management

        #region Push
        /// <summary>
        /// Push form to top of stack.
        /// </summary>
        /// <param name="formType">Type of form to push</param>
        public void Push(Type formType)
        {
            Form currentForm = null;

            // Only allow 1 Push at a time to maintain stack and stack integrity
            Monitor.Enter(this);

            Cursor.Current = Cursors.WaitCursor;

            // Find parent (current) form
            if (stack.Count < 1)
            {
                currentForm = rootForm;
            }
            else
            {
                foreach (Form f in List)
                {
                    // Find the last form in the stack
                    if (f.GetType().Name.Equals(stack[stack.Count - 1]))
                    {
                        currentForm = f;
                        break;
                    }
                }
            }
            // Stack new form
            activation = false;
            Form form = Load(formType);
            activation = true;
            form.Visible = true;
            stack.Add(formType.Name);
            currentForm.Visible = false;
            activation = false;

            Monitor.Exit(this);
            Cursor.Current = Cursors.Default;
        }
        #endregion

        #region Pop
        /// <summary>
        /// Hides/pops current visible form in stack.
        /// </summary>
        /// <param name="formsToPop"></param>
        public void Pop(int formsToPop)
        {
            activation = true;

            // Remove all forms in cache?
            if (formsToPop >= stack.Count)
            {
                stack.Clear();
                Form currentForm = null;
                foreach (Form f in List)
                {
                    // Current form?
                    if (f.Visible)
                    {
                        currentForm = f;
                        break;
                    }
                }
                rootForm.Visible = true;
                currentForm.Visible = false;
            }
            else
            {
                // Remove from stack but not from cache
                for (int i = 0; i < formsToPop; i++)
                {
                    stack.RemoveAt(stack.Count - 1);
                }

                Form currentForm = null;
                foreach (Form f in List)
                {
                    // Current form?
                    if (f.Visible)
                        currentForm = f;

                    // Last form in the stack?
                    if (f.GetType().Name.Equals(stack[stack.Count - 1]))
                        f.Visible = true;
                }
                currentForm.Visible = false;
            }
            activation = false;
        }
        #endregion

        #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
Team Leader
Singapore Singapore
- B.Sc. degree in Computer Science.
- 4+ years experience in Visual C#.net and VB.net
- Obsessed in OOP style design and programming.
- Designing and developing Network security tools.
- Designing and developing a client/server application for sharing files among users in a way other than FTP protocol.
- Designing and implementing GSM gateway applications and bulk messaging.
- Windows Mobile and Symbian Programming
- Having knowledge with ERP solutions

The summary of my skills:
C#, VB.Net#,ASP.net, VC++, Java, WPF,WCF, Oracle, SQL Server, MS Access, Windows NT administration

Cheers
RRave
MCPD,MCTS
http://codegain.com

Comments and Discussions