Click here to Skip to main content
15,885,767 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 .IO;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace UserDrivenApplications
{
    public partial class Form_DataWorld : Form 
    {
        // -------------------------------------------------        EnableGroup_Arrays
        private void EnableGroup_Arrays (bool bEnable)
        {
            if (bEnable == false)
            {
                groupXYArrays .Title = "Arrays";
                ClearGroup_Arrays ();
            }
            foreach (Control control in ctrlsListArrays)
            {
                control .Enabled = bEnable;
            }
            EnableGroup_EditXYArrays (bEnable);
        }
        // -------------------------------------------------        EnableGroup_EditXYArrays
        private void EnableGroup_EditXYArrays (bool bEnable)
        {
            foreach (Control control in ctrlsEditABArray)
            {
                control .Enabled = bEnable;
            }
            if (bEnable == false)
            {
                groupEditXYArray .Title = "";
                ClearGroup_EditXYArrays ();
            }
        }
        // -------------------------------------------------        ClearGroup_Arrays
        private void ClearGroup_Arrays ()
        {
            listABArrays .Items .Clear ();
            textNewABArrayName .Text = "";

            ClearGroup_EditXYArrays ();
        }
        // -------------------------------------------------        ClearGroup_EditXYArrays
        private void ClearGroup_EditXYArrays ()
        {
            if (groupEditXYArray .MSPlot != null)
            {
                groupEditXYArray .MSPlot .CloseTuningForms ();
            }
            groupEditXYArray .Title = "";
            textABArray_Xrange .Text = "";
            textABArray_Yrange .Text = "";
            textString_Comment .Text = "";
            datagridXY .Rows .Clear ();
            groupEditXYArray .Dots .Clear ();
            groupEditXYArray .DotNest .Visible = false;
        }
        // -------------------------------------------------        KeyPress_textNewABArrayName
        private void KeyPress_textNewABArrayName (object sender, KeyPressEventArgs e)
        {
            TextBox txtbox = (TextBox) sender;
            if ((int) e .KeyChar == (int) Keys .Enter)
            {
                Data_Page page = SelectedPage;

                Data_Set set = SelectedSet;
                string strNew = txtbox .Text .Trim ();
                if (string .IsNullOrEmpty (strNew))
                {
                    MessageBox .Show ("No name for the new Array", "New array", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                }
                else
                {
                    for (int i = 0; i < set .ABArrays .Count; i++)
                    {
                        if (strNew == set .ABArrays [i] .Name)
                        {
                            MessageBox .Show ("Array with such name already exists in this Set; select another name", "New array",
                                              MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                            return;
                        }
                    }
                    MSPlot plotNew = new MSPlot (this, groupEditXYArray .MSPlot .MainArea .Area);  
                    Data_ABArray arrayNew = new Data_ABArray (strNew, set .ShowToUser, plotNew); 
                    set .AddABArray (arrayNew);
                    SetsList_RefreshSelectShow (listSets .SelectedIndices [0]);

                    FillABArraysList ();
                    bool bSel = Auxi_Common .ListView_LineSelectAndShow (listABArrays, listABArrays .Items .Count - 1);
                }
            }
        }
        // -------------------------------------------------        FillABArraysList
        private void FillABArraysList ()
        {
            ListView list = listABArrays;
            list .Items .Clear ();
            if (PageAndSetSelected)
            {
                Data_Set set = SelectedSet;
                if (set .SetType == SetType .ABArrays)
                {
                    for (int i = 0; i < set .ABArrays .Count; i++)
                    {
                        Data_ABArray data = set .ABArrays [i];
                        string [] strs = new string [2];
                        strs [0] = data .Name;
                        strs [1] = data .ShowToUser ? "Yes" : "No";
                        ListViewItem lvi = new ListViewItem (strs);
                        list .Items .Add (lvi);
                    }
                    groupEditXYArray .DotNest .Visible = true;
                }
                RenewMover ();
            }
        }
        // -------------------------------------------------        SelectedIndexChanged_listABArrays
        private void SelectedIndexChanged_listABArrays (object sender, EventArgs e)
        {
            ListView list = listABArrays;   // sender as ListView;

            if (PageAndSetSelected)
            {
                if (list .SelectedIndices .Count > 0)
                {
                    Data_Set set = SelectedSet;
                    int iArray = list .SelectedIndices [0];
                    Data_ABArray data = set .ABArrays [iArray];
                    groupEditXYArray .Title = "Array  " + data .Name;
                    if (data .UseRange_A)
                    {
                        textABArray_Xrange .Text = data .A_minimum .ToString ("F3") + ", " + data .A_maximum .ToString ("F3");
                    }
                    else
                    {
                        textABArray_Xrange .Text = "";
                    }

                    if (data .UseRange_B)
                    {
                        textABArray_Yrange .Text = data .B_minimum .ToString ("F3") + ", " + data .B_maximum .ToString ("F3");
                    }
                    else
                    {
                        textABArray_Yrange .Text = "";
                    }
                    textABArray_Comment .Text = data .Comment;
                    ABArray_IntoTable (data);
                    groupEditXYArray .MSPlot .CopyView (data .MSPlot);

                    groupEditXYArray .Dots = new DotsOnPlot (groupEditXYArray .MSPlot, data .Pairs);
                    groupEditXYArray .DotNest .Visible = true;
                    EnableGroup_EditXYArrays (true);

                    btnDeleteABArray .Enabled = true;
                    btnShowHideABArray .Enabled = true;
                    btnABArrayUp .Enabled = iArray > 0;
                    btnABArrayDown .Enabled = iArray < list .Items .Count - 1;
                }
                else
                {
                    DisableFourButtons_ABArrays ();
                    EnableGroup_EditXYArrays (false);
                }
            }
            else
            {
                ClearGroup_EditXYArrays ();
            }
            groupXYArrays .Update ();
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        DisableFourButtons_ABArrays
        private void DisableFourButtons_ABArrays ()
        {
            btnDeleteABArray .Enabled = false;
            btnShowHideABArray .Enabled = false;
            btnABArrayUp .Enabled = false;
            btnABArrayDown .Enabled = false;
        }
        // -------------------------------------------------        AfterLabelEdit_listABArrays
        private void AfterLabelEdit_listABArrays (object sender, LabelEditEventArgs e)
        {
            if (string .IsNullOrEmpty (e .Label) || string .IsNullOrEmpty (e .Label .Trim ()))
            {
                e .CancelEdit = true;
                return;
            }
            string str = e .Label .Trim ();
            ListView list = sender as ListView;
            int iSelLine = list .SelectedIndices [0];
            Data_Set set = SelectedSet;
            for (int i = 0; i < list .Items .Count; i++)
            {
                if (i != iSelLine)
                {
                    if (str == set .ABArrays [i] .Name)
                    {
                        e .CancelEdit = true;
                        MessageBox .Show ("Array with such name already exists; select another name", "Rename array",
                                          MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                        return;
                    }
                }
            }
            set .ABArrays [iSelLine] .Name = str;
            list .Items [iSelLine] .Selected = true;
            textNewABArrayName .Text = str;
        }
        // -------------------------------------------------        Click_btnDeleteABArray
        private void Click_btnDeleteABArray (object sender, EventArgs e)
        {
            if (PageAndSetSelected)
            {
                ListView list = listABArrays;
                if (list .SelectedIndices .Count > 0)
                {
                    if (DialogResult .Yes == MessageBox .Show ("Do you want to delete the selected array?", "Delete array",
                                                   MessageBoxButtons .YesNo, MessageBoxIcon .Question, MessageBoxDefaultButton .Button1))
                    {
                        int iSetSelected = listSets .SelectedIndices [0];
                        Data_Set set = SelectedSet;
                        int iArraySelected = list .SelectedIndices [0];
                        set .DeleteABArray (iArraySelected);

                        SetsList_RefreshSelectShow (iSetSelected);
                        FillABArraysList ();
                        if (set .ABArrays .Count > 0)
                        {
                            Auxi_Common .ListView_LineSelectAndShow (list, Math .Min (iArraySelected, set .ABArrays .Count - 1));
                        }
                    }
                }
                else
                {
                    MessageBox .Show ("Array to delete is not selected", "Delete array", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                }
            }
        }
        // -------------------------------------------------        CheckPairs
        private List<Data_ABPair> CheckPairs (ref bool useArange, ref double minA, ref double maxA,
                                              ref bool useBrange, ref double minB, ref double maxB)
        {
            List<Data_ABPair> pairs = PairsFromTable ();
            if (pairs .Count < 1)
            {
                MessageBox .Show ("There is not a single pair of values", "XY array", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                return (null);
            }
            // ranges from the table
            double minX, maxX, minY, maxY;
            minX = maxX = pairs [0] .A_value;
            minY = maxY = pairs [0] .B_value;
            for (int i = 1; i < pairs .Count; i++)
            {
                minX = Math .Min (minX, pairs [i] .A_value);
                maxX = Math .Max (maxX, pairs [i] .A_value);
                minY = Math .Min (minY, pairs [i] .B_value);
                maxY = Math .Max (maxY, pairs [i] .B_value);
            }
            // ranges from textBoxes
            char [] separators = new char [] { ' ', ',' };
            // A_range
            if (string .IsNullOrEmpty (textABArray_Xrange .Text))
            {
                useArange = false;
            }
            else
            {
                useArange = true;
                double [] aRange = Auxi_Convert .GetDoubles (textABArray_Xrange .Text, separators);
                if (aRange != null && aRange .Length >= 2)
                {
                    minA = Math .Min (aRange [0], aRange [1]);
                    maxA = Math .Max (aRange [0], aRange [1]);
                    if (minX < minA || maxA < maxX)
                    {
                        MessageBox .Show ("X values in the table are out of the declared range", "XY array", 
                                          MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                        return (null);
                    }
                }
                else
                {
                    MessageBox .Show ("A mistake in the X range; check the string", "XY array", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                    return (null);
                }
            }
            // B_range
            if (string .IsNullOrEmpty (textABArray_Yrange .Text))
            {
                useBrange = false;
            }
            else
            {
                useBrange = true;
                double [] bRange = Auxi_Convert .GetDoubles (textABArray_Yrange .Text, separators);
                if (bRange != null && bRange .Length >= 2)
                {
                    minB = Math .Min (bRange [0], bRange [1]);
                    maxB = Math .Max (bRange [0], bRange [1]);
                    if (minY < minB || maxB < maxY)
                    {
                        MessageBox .Show ("Y values in the table are out of the declared range", "XY array", 
                                          MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                        return (null);
                    }
                }
                else
                {
                    MessageBox .Show ("A mistake in the Y range; check the string", "XY array", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                    return (null);
                }
            }
            return (pairs);
        }
        // -------------------------------------------------        Click_btnShowHideABArray
        private void Click_btnShowHideABArray (object sender, EventArgs e)
        {
            if (PageAndSetSelected)
            {
                ListView list = listABArrays;
                if (list .SelectedIndices .Count > 0)
                {
                    Data_Page page = SelectedPage;
                    int iSetSelected = listSets .SelectedIndices [0];
                    Data_Set set = SelectedSet;
                    int iArraySelected = list .SelectedIndices [0];
                    Data_ABArray data = set .ABArrays [iArraySelected];
                    data .ShowToUser = !data .ShowToUser;

                    SetsList_RefreshSelectShow (iSetSelected);
                    Auxi_Common .ListView_LineSelectAndShow (listABArrays, iArraySelected);
                }
                else
                {
                    MessageBox .Show ("Array is not selected", "XY arrays", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                }
            }
        }
        // -------------------------------------------------        Click_btnABArrayUp
        private void Click_btnABArrayUp (object sender, EventArgs e)
        {
            if (PageAndSetSelected)
            {
                ListView list = listABArrays;
                if (list .SelectedIndices .Count > 0)
                {
                    int iArraySelected = list .SelectedIndices [0];
                    if (iArraySelected > 0)
                    {
                        SelectedSet .ABArrays .Reverse (iArraySelected - 1, 2);
                        iArraySelected--;
                        FillABArraysList ();
                        Auxi_Common .ListView_LineSelectAndShow (list, iArraySelected);
                    }
                }
                else
                {
                    MessageBox .Show ("Array is not selected", "XY arrays", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                }
            }
        }
        // -------------------------------------------------        Click_btnABArrayDown
        private void Click_btnABArrayDown (object sender, EventArgs e)
        {
            if (PageAndSetSelected)
            {
                ListView list = listABArrays;
                if (list .SelectedIndices .Count > 0)
                {
                    Data_Set set = SelectedSet;
                    int iArraySelected = list .SelectedIndices [0];
                    if (iArraySelected < set .ABArrays .Count - 1)
                    {
                        set .ABArrays .Reverse (iArraySelected, 2);
                        iArraySelected++;
                        FillABArraysList ();
                        Auxi_Common .ListView_LineSelectAndShow (list, iArraySelected);
                    }
                }
                else
                {
                    MessageBox .Show ("Array is not selected", "XY arrays", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                }
            }
        }
        // -------------------------------------------------        PairsFromTable
        private List<Data_ABPair> PairsFromTable ()
        {
            double x, y;
            List<Data_ABPair> pairs = new List<Data_ABPair> ();
            for (int i = 0; i < datagridXY .Rows .Count; i++)
            {
                if (datagridXY .Rows [i] .Cells [0] .Value != null &&
                    datagridXY .Rows [i] .Cells [1] .Value != null &&
                    double .TryParse (datagridXY .Rows [i] .Cells [0] .Value .ToString (), out x) &&
                    double .TryParse (datagridXY .Rows [i] .Cells [1] .Value .ToString (), out y))
                {
                    pairs .Add (new Data_ABPair (x, y));
                }
            }
            return (pairs);
        }
        // -------------------------------------------------        ABArray_InsertTableLine
        private void ABArray_InsertTableLine (int iDot)
        {
            string [] strs = new string [] {groupXYArrays .Dots .Args [iDot] .ToString ("F3"), 
                                            groupXYArrays .Dots .Vals [iDot] .ToString ("F4") };
            datagridXY .Rows .Insert (iDot, strs);
            datagridXY .Rows [iDot] .Selected = true;
        }
        // -------------------------------------------------        ABArray_RenewTableLine
        private void ABArray_RenewTableLine (int iDot)
        {
            string [] strs = new string [] {groupXYArrays .Dots .Args [iDot] .ToString ("F3"), 
                                            groupXYArrays .Dots .Vals [iDot] .ToString ("F4") };
            datagridXY .Rows [iDot] .SetValues (strs);
            datagridXY .Rows [iDot] .Selected = true;
        }
        // -------------------------------------------------        ABArray_IntoTable
        private void ABArray_IntoTable (Data_ABArray data)
        {
            datagridXY .Rows .Clear ();
            for (int i = 0; i < data .Pairs .Count; i++)
            {
                string [] strs = new string [] { data .Pairs [i] .A_value .ToString ("F3"), data .Pairs [i] .B_value .ToString ("F4") };
                datagridXY .Rows .Insert (i, strs);
            }
        }
        // -------------------------------------------------        CellEndEdit_datagridXY
        private void CellEndEdit_datagridXY (object sender, DataGridViewCellEventArgs e)
        {
            int row = e .RowIndex;
            int col = e .ColumnIndex;
            int nRows = datagridXY .RowCount;
            bool bRenewGrid = false;
            if (0 <= row && row < nRows - 1 && (col == 0 || col == 1))
            {
                double valueNew;
                if (double .TryParse (datagridXY .CurrentCell .Value .ToString (), out valueNew))
                {
                    int colAnother = (col == 0) ? 1 : 0;
                    double valueAnother;
                    if (datagridXY .Rows [row] .Cells [colAnother] .Value != null &&
                        double .TryParse (datagridXY .Rows [row] .Cells [colAnother] .Value .ToString (), out valueAnother))
                    {
                        List<Data_ABPair> pairsNew = PairsFromDatagrid ();
                        pairsNew .Sort (ComparePairs);
                        groupXYArrays .Dots .Pairs = pairsNew;

                        if (listABArrays .Items .Count > 0 && listABArrays .SelectedIndices .Count > 0)
                        {
                            Data_Set set = SelectedSet;
                            Data_ABArray array = set .ABArrays [listABArrays .SelectedIndices [0]];
                            array .Pairs = pairsNew;
                        }
                        bRenewGrid = true;
                    }
                }
                else
                {
                    if (0 <= row && row < groupXYArrays .Dots .Args .Count)
                    {
                        ABArray_RenewTableLine (row);
                    }
                }
            }
            if (groupXYArrays .Dots .Args .Count >= 1 && bRenewGrid)
            {
                RenewGridFromDots ();
            }
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        UserDeletingRow_datagridXY
        private void UserDeletingRow_datagridXY (object sender, DataGridViewRowCancelEventArgs e)
        {
            DataGridViewRow row = e .Row;
            int iRow = row .Index;
            
            if (iRow < datagridXY .Rows .Count - 1)
            {
                SelectedABArray .Pairs .RemoveAt (iRow);
                groupXYArrays .Dots .Pairs = SelectedABArray .Pairs;
                RenewMover ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        RenewGridFromDots
        private void RenewGridFromDots ()
        {
            int nDots = groupXYArrays .Dots .Args .Count;
            for (int i = 0; i < nDots; i++)
            {
                string [] strs = new string [] {groupXYArrays .Dots .Args [i] .ToString ("F3"), 
                                                groupXYArrays .Dots .Vals [i] .ToString ("F4") };
                datagridXY .Rows [i] .SetValues (strs);
            }
            for (int i = datagridXY .Rows .Count - 2; i >= nDots; i--)
            {
                datagridXY .Rows .RemoveAt (i);
            }
        }
        // -------------------------------------------------        PairsFromDatagrid
        private List<Data_ABPair> PairsFromDatagrid ()
        {
            double x, y;
            List<Data_ABPair> pairsRead = new List<Data_ABPair> ();
            for (int i = 0; i < datagridXY .Rows .Count - 1; i++)
            {
                if (datagridXY .Rows [i] .Cells [0] .Value != null &&
                    datagridXY .Rows [i] .Cells [1] .Value != null &&
                    double .TryParse (datagridXY .Rows [i] .Cells [0] .Value .ToString (), out x) &&
                    double .TryParse (datagridXY .Rows [i] .Cells [1] .Value .ToString (), out y))
                {
                    pairsRead .Add (new Data_ABPair (x, y));
                }
            }
            return (pairsRead);
        }
        // -------------------------------------------------        ComparePairs
        private int ComparePairs (Data_ABPair pair_0, Data_ABPair pair_1)
        {
            if (pair_0 .A_value < pair_1 .A_value)
            {
                return (-1);
            }
            else if (pair_0 .A_value > pair_1 .A_value)
            {
                return (1);
            }
            else
            {
                if (pair_0 .B_value < pair_1 .B_value)
                {
                    return (-1);
                }
                else if (pair_0 .B_value > pair_1 .B_value)
                {
                    return (1);
                }
                else
                {
                    return (0);
                }
            }
        }
        // -------------------------------------------------        KeyPress_textABArrayComment
        private void KeyPress_textABArrayComment (object sender, KeyPressEventArgs e)
        {
            TextBox txtbox = (TextBox) sender;
            if ((int) e .KeyChar == (int) Keys .Enter)
            {
                SelectedABArray .Comment = txtbox .Text .Trim ();
            }
        }
    }
}

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