Try it yourself

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.IO; //needed for file save

namespace Sample3
{
    public partial class Form1 : Form
    {// to create the Array but without defining the number of inputs
        private string[,] contractorSectors;

        private string[,] contractorProfessions;

        private string[] sectors = { “Pharmaceuticals”, “Banking”, “Software” };

        private string[] professions = { “Accounting”, “Marketing”, “HR”, “IT” };

        private int noContractors = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxProfession.Items.AddRange
                (new string[] { “Accounting”, “Marketing”, “HR”, “IT” });
            comboBoxSector.Items.AddRange
                (new string[] { “Pharmaceuticals”, “Banking”, “Software” });
        }

        //exit the app code
        private void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //to clear all inputted data.
        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxName.Text = “”;
            comboBoxSector.SelectedIndex = -1;
            comboBoxProfession.SelectedIndex = -1;
            listViewProfession.Items.Clear();
            listViewSector.Items.Clear();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                string contractorName = textBoxName.Text;

                if (contractorName == “”)
                {
                    throw new InvalidOperationException
                        (“The contractor name cannot be empty”);
                }

                int selectedSector = comboBoxSector.SelectedIndex;
                int selectedProfession = comboBoxProfession.SelectedIndex;

                noContractors++;

                string[,] tmpContractorSectors = contractorSectors;//tmp = temporary arrays
                string[,] tmpContractorProfession = contractorProfessions; //ditto

                contractorSectors = new string[3, noContractors];
                contractorProfessions = new string[4, noContractors];

                if (tmpContractorSectors != null)
                {
                    for (int row = 0; row < 3; row++)
                    {
                        for (int column = 0; column < (noContractors – 1); column++)
                        {
                            if (tmpContractorSectors[row, column] == null)
                            {
                                continue;
                            }

                            contractorSectors[row, column] = //puts the temp entries into the arrary
                                tmpContractorSectors[row, column];
                        }

                    }
                }

                if (tmpContractorProfession != null)
                {
                    for (int row = 0; row < 4; row++)
                    {
                        for (int column = 0; column < (noContractors – 1); column++)
                        {
                            if (tmpContractorProfession[row, column] == null)
                            {
                                continue;
                            }

                            contractorProfessions[row, column] =
                                tmpContractorProfession[row, column];

                        }
                    }
                }

                contractorSectors[selectedSector, noContractors – 1] = contractorName; //nocontractors -1 means going to the last entry
                contractorProfessions[selectedProfession, noContractors – 1] = contractorName;
            }

            catch (InvalidOperationException ioE)
            {
                MessageBox.Show(ioE.Message, “Error”);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, “Error”);
            }
        }

        private void buttonDisplay_Click(object sender, EventArgs e)
        {
            listViewProfession.Items.Clear();
            listViewSector.Items.Clear();

            for (int row = 0; row < 3; row++)
            {
                for (int column = 0; column < noContractors; column++)
                {
                    if (contractorSectors[row, column] == “” ||
                        contractorSectors[row, column] == null)
                    {
                        continue;
                    }

                    string sector = “”;
                    if (row == 0)
                    {
                        sector = “Pharmaceuticals”;
                    }
                    else if (row == 1)
                    {
                        sector = “Banking”;
                    }
                    else if (row == 2)
                    {
                        sector = “Software”;
                    }

                    ListViewItem item = new ListViewItem();
                    item = listViewSector.Items.Add(sector);
                    item.SubItems.Add(contractorSectors[row, column]);

                }
            }

            for (int row = 0; row < 4; row++)
            {
                for (int column = 0; column < noContractors; column++)
                {
                    if (contractorProfessions[row, column] == “” || contractorProfessions[row, column] == null)
                    {
                        continue;
                    }

                    string profession = “”;
                    if (row == 0)
                    {
                        profession = “Accounting”;
                    }
                    else if (row == 1)
                    {
                        profession = “Marketing”;
                    }
                    else if (row == 2)
                    {
                        profession = “HR”;
                    }
                    else if (row == 3)
                    {
                        profession = “IT”;
                    }

                    ListViewItem item = new ListViewItem();
                    item = listViewProfession.Items.Add(profession);
                    item.SubItems.Add(contractorProfessions[row, column]);
                }
            }
        }

        private void buttonFileSave_Click(object sender, EventArgs e)
        {
            TextWriter writer = null;
            try
            {
                FileStream stream = File.Open(“E:\\contractors.dat”,//file name I’ve assigned for this solution
                    FileMode.OpenOrCreate);//code used to access or create the file that will be writen to.
                writer = new StreamWriter(stream);

                for (int row = 0; row< 3; row++)
                {
                    for (int column = 0; column < noContractors; column++)
                    {
                        if (contractorSectors[row, column] == “”||
                            contractorSectors[row, column] == null)

                        {
                            continue;
                        }

                        string sector = “”;
                        if (row==0)
                        {
                            sector = “Pharmaceuticals”;
                        }
                        else if (row==1)
                        {
                            sector= “Banking”;
                        }
                        else if (row==2)
                        {
                            sector = “Software”;
                        }

                    writer.Write(sector);// code used to write to file
                    writer.Write(” “);
                    writer.Write(contractorSectors[row,column]);
                    writer.WriteLine();
                    writer.Flush();//flush is the command to transfer the data to the file
                    }

                }

                for (int row = 0; row<4; row++)
                {
                    for (int column = 0; column<noContractors; column++)
                    {
                        if (contractorProfessions[row,column]== “”
                            || contractorProfessions[row,column] == null)
                        {
                            continue;
                        }

                        string profession = “”;
                        if (row==0)
                        {
                            profession = “Accounting”;
                        }
                        else if (row ==1)
                        {
                            profession = “Marketing”;
                        }
                        else if (row ==2)
                        {
                            profession= “HR”;
                        }
                        else if (row==3)
                        {
                            profession = “IT”;
                        }

                        writer.Write(profession);
                        writer.Write(” “);
                        writer.Write(contractorProfessions[row,column]);
                        writer.WriteLine();
                        writer.Flush();//must have () after Flush command
                         }
            
        
                    }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, “Error”);
            }

            finally //forces the writer to close regardless of an exception being thrown or not.
                //needed to release the file handler due to finite resources.
            {
                writer.Close();
            }
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s