Click here to Skip to main content
15,886,701 members
Articles / Programming Languages / C#

User-driven applications

Rate me:
Please Sign up or sign in to vote.
4.88/5 (24 votes)
10 Apr 2010CPOL136 min read 33K   5   78  
User-driven applications are the programs in which full control is given to the users. Designers of such programs are responsible only for developing an instrument for solving some task, but they do not enforce users to work with this instrument according with a predefined scenario.
using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace UserDrivenApplications
{
    public partial class Form_YearsSelection : Form
    {
        const string strAddRegKey = "Form_YearsSelection";
        Mover mover;
        Group groupAll, groupSelected;
        string [] titles = new string [] { "All years", "Selected" };
        Size [] sizeTitles;
        Spaces spaces;

        // -------------------------------------------------
        public Form_YearsSelection ()
        {
            InitializeComponent ();
            mover = new Mover (this);

            for (int i = 1880; i <= DateTime .Now .Year; i++)
            {
                listYearsAll .Items .Add (i .ToString ());
            }

            sizeTitles = Auxi_Geometry .RoundMeasureStrings (this, titles);
            spaces = new Spaces (this); 

            Rectangle rc = Auxi_Geometry .FrameAroundControls (new Control [] { listYearsAll, btnSelect }, spaces);
            groupAll = new Group (this, rc, new RectRange (sizeTitles [0] .Width + 20, 180, 100, 500), titles [0], MoveAll);
            rc = Auxi_Geometry .FrameAroundControls (new Control [] { listYearsSelected, btnExclude }, spaces);
            groupSelected = new Group (this, rc, new RectRange (sizeTitles [1] .Width + 20, 180, 100, 500), titles [1], MoveSelected);

            RestoreFromRegistry ();
            RenewMover ();
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        RenewMover
        // order:  btnOK - groupSelected - groupAll 
        //
        public void RenewMover ()
        {
            mover .Clear ();
            mover .Insert (0, groupAll);
            mover .Insert (0, groupSelected);
            mover .Insert (0, btnOK);
        }
        // -------------------------------------------------        MoveAll
        void MoveAll ()
        {
            Rectangle rc = groupAll .RectAround;
            int cxL = rc .Left + spaces .Left_inFrame;
            int cxR = rc .Right - spaces .Right_inFrame;
            btnSelect .Location = new Point (cxL, rc .Bottom - (spaces .Btm_inFrame + btnSelect .Height));
            btnSelect .Width = cxR - cxL;

            int cyT = rc .Top + spaces .Top_inFrame;
            int cyB = btnSelect .Top - spaces .VerMin;
            listYearsAll .Bounds = new Rectangle (cxL, cyT, btnSelect .Width, cyB - cyT);
            listYearsAll .Columns [0] .Width = listYearsAll .Width - (SystemInformation .VerticalScrollBarWidth + 4);
        }
        // -------------------------------------------------        MoveSelected
        void MoveSelected ()
        {
            Rectangle rc = groupSelected .RectAround;
            int cxL = rc .Left + spaces .Left_inFrame;
            int cxR = rc .Right - spaces .Right_inFrame;
            btnExclude .Location = new Point (cxL, rc .Bottom - (spaces .Btm_inFrame + btnExclude .Height));
            btnExclude .Width = cxR - cxL;

            int cyT = rc .Top + spaces .Top_inFrame;
            int cyB = btnExclude .Top - spaces .VerMin;
            listYearsSelected .Bounds = new Rectangle (cxL, cyT, btnExclude .Width, cyB - cyT);
            listYearsSelected .Columns [0] .Width = listYearsSelected .Width - (SystemInformation .VerticalScrollBarWidth + 4);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            mover .Catch (e .Location, e .Button);
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            mover .Release ();
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            groupAll .Draw (grfx);
            groupSelected .Draw (grfx);
        }
        // -------------------------------------------------        Click_btnSelect
        private void Click_btnSelect (object sender, EventArgs e)
        {
            if (listYearsAll .SelectedIndices .Count > 0)
            {
                for (int i = 0; i < listYearsAll .SelectedIndices .Count; i++)
                {
                    string str = listYearsAll .Items [listYearsAll .SelectedIndices [i]] .Text;
                    if (null == listYearsSelected .FindItemWithText (str))
                    {
                        listYearsSelected .Items .Add (str);
                    }
                }
            }
        }
        // -------------------------------------------------        Click_btnExclude
        private void Click_btnExclude (object sender, EventArgs e)
        {
            for (int i = listYearsSelected .SelectedIndices .Count - 1; i >= 0; i--)
            {
                listYearsSelected .Items .RemoveAt (listYearsSelected .SelectedIndices [i]);
            }
        }

        const string nameLocation = "Location";
        // -------------------------------------------------        SaveInfoToRegistry
        private void SaveInfoToRegistry ()
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strkey);
                if (regkey != null)
                {
                    string [] strSizes = {  ClientSize .Width .ToString (),         // 0            
                                            ClientSize .Height .ToString (),        // 1
                                            groupAll .RectAround .X .ToString (),   // 2    
                                            groupAll .RectAround .Y .ToString (),   // 3    
                                            groupAll .Width .ToString (),           // 4    
                                            groupAll .Height .ToString (),          // 5    
                                            groupSelected .RectAround .X .ToString (),  // 6    
                                            groupSelected .RectAround .Y .ToString (),  // 7 
                                            groupSelected .Width .ToString (),      // 8    
                                            groupSelected .Height .ToString (),     // 9
                                            btnOK .Left .ToString (),               // 10
                                            btnOK .Top .ToString (),                // 11 
                                        };
                    regkey .SetValue (nameLocation, strSizes, RegistryValueKind .MultiString);
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
        // -------------------------------------------------        RestoreFromRegistry
        private void RestoreFromRegistry ()
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .OpenSubKey (strkey);
                if (regkey != null)
                {
                    string [] strSizes = (string []) regkey .GetValue (nameLocation);
                    if (strSizes != null && strSizes .Length == 12)
                    {
                        ClientSize = Auxi_Convert .ToSize (strSizes, 0);
                        groupAll .RectAround = Auxi_Convert .ToRectangle (strSizes, 2);
                        groupSelected .RectAround = Auxi_Convert .ToRectangle (strSizes, 6);
                        btnOK .Location = Auxi_Convert .ToPoint (strSizes, 10);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
    }
}

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

Comments and Discussions