private void rdAddProducts_CheckedChanged(object sender, EventArgs e)
 {
     if (rdAddProducts.Checked == true)
     {
         int                  rowIndex    = dgPackageProductSuppliers.CurrentRow.Index; //to keep state at return
         int                  rowIndexSec = dgProd.CurrentRow.Index;                    //to keep data grid state at return
         Products             product;
         frmAddModifyProducts addProductSuppForm = new frmAddModifyProducts();
         addProductSuppForm.rowIndex    = rowIndexSec; //to initialize the combobox
         addProductSuppForm.editProdSup = true;        //make sure the edit product suppliers form is open
         addProductSuppForm.suppliers   = suppliers;
         addProductSuppForm.products    = products;
         DialogResult resultProducts = addProductSuppForm.ShowDialog();
         if (resultProducts == DialogResult.OK) //if result is ok, a new product was addedd
         {
             product = addProductSuppForm.product;
             products2.Add(product);
             rowIndexSec = products2.Count - 1; // so reduce list index
             this.displayAll(rowIndex, rowIndexSec);
         }
         else if (resultProducts == DialogResult.Yes) //if yes, an existing product was selected
         {
             product = addProductSuppForm.product;
             this.displayAll(rowIndex, rowIndexSec); //so just keep index refreshed
         }
         rdAddProducts.Checked = false;              //remove the check after
     }
 }
        /// <summary>
        /// On clicking edit button
        /// react based on current tab
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            switch (tbMain.SelectedTab.Name)
            {
            case "tabPackages":
                Package             package           = packages[dgPackages.CurrentRow.Index]; //take the currently selected package
                frmAddModifyPackage modifyPackageForm = new frmAddModifyPackage();
                modifyPackageForm.addPackage = false;                                          //create the package form in edit mode
                modifyPackageForm.package    = package;
                DialogResult result = modifyPackageForm.ShowDialog();
                if (result == DialogResult.OK)     //if result comes back ok
                {
                    package = modifyPackageForm.package;
                    packages[dgPackages.CurrentRow.Index] = package;     //replace the selected row with the reult from form
                    this.displayAll();
                }
                else if (result == DialogResult.Retry)     //else stay the same
                {
                    this.displayAll();
                }
                break;

            case "tabProducts":
                Products             product           = products[dgProducts.CurrentRow.Index];
                frmAddModifyProducts modifyProductForm = new frmAddModifyProducts();
                modifyProductForm.addProducts = false;
                modifyProductForm.product     = product;
                modifyProductForm.products    = products;
                DialogResult resultProduct = modifyProductForm.ShowDialog();
                if (resultProduct == DialogResult.OK)
                {
                    product = modifyProductForm.product;
                    products[dgProducts.CurrentRow.Index] = product;
                    this.displayAll();
                }
                else if (resultProduct == DialogResult.Retry)
                {
                    this.displayAll();
                }

                break;

            case "tabSuppliers":
                Suppliers             supplier           = suppliers[dgSuppliers.CurrentRow.Index];
                frmAddModifySuppliers modifySupplierForm = new frmAddModifySuppliers();
                modifySupplierForm.addSupplier = false;
                modifySupplierForm.supplier    = supplier;
                DialogResult resultSupplier = modifySupplierForm.ShowDialog();
                if (resultSupplier == DialogResult.OK)
                {
                    supplier = modifySupplierForm.supplier;
                    suppliers[dgSuppliers.CurrentRow.Index] = supplier;
                    this.displayAll();
                }
                else if (resultSupplier == DialogResult.Retry)
                {
                    this.displayAll();
                }

                break;

            //On homepage
            case "tabPackageProductSupplier":
                int                 rowIndex              = dgPackageProductSuppliers.CurrentRow.Index; //Preserve application state
                Package             packageHome           = packages[rowIndex];                         // Take data from packages list
                frmAddModifyPackage modifyPackageHomeForm = new frmAddModifyPackage();                  //Instatiate new form
                modifyPackageHomeForm.addPackage = false;                                               // In special add package mode
                modifyPackageHomeForm.package    = packageHome;
                DialogResult resultHome = modifyPackageHomeForm.ShowDialog();
                if (resultHome == DialogResult.OK)     //If return is OK
                {
                    package            = modifyPackageHomeForm.package;
                    packages[rowIndex] = package;          //replace package with result from form
                    this.displayAll(rowIndex);             //keep state
                }
                else if (resultHome == DialogResult.Retry) //if retry, just reload and keep state
                {
                    this.displayAll(rowIndex);
                }
                break;
            }
        }
        /// <summary>
        /// On clicking the add button, react based on tab selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            switch (tbMain.SelectedTab.Name)
            {
            case "tabPackages":
                Package             package;                                    //create a new package object
                frmAddModifyPackage addPackageForm = new frmAddModifyPackage(); //create new form instance
                addPackageForm.addPackage = true;                               //make it an add form
                DialogResult resultPackage = addPackageForm.ShowDialog();
                if (resultPackage == DialogResult.OK)                           //if result comes back as ok
                {
                    //add a new package with what form returns
                    package = addPackageForm.package;
                    packages.Add(package);
                    this.displayAll();                                                                                                                  //refresh the tables
                    DialogResult dia = MessageBox.Show("Would you like to add products to this package now?", "Add Products", MessageBoxButtons.YesNo); //Extra dialog to send user to home page
                    if (dia == DialogResult.Yes)
                    {
                        tbMain.SelectedTab = tabPackageProductSupplier;
                    }
                }
                break;

            case "tabProducts":
                Products             product;                                     //Create new products object
                frmAddModifyProducts addProductForm = new frmAddModifyProducts(); //Instantiate new Form
                addProductForm.addProducts = true;                                // set add mode to true
                addProductForm.suppliers   = suppliers;                           //Pass suppliers array to form
                addProductForm.products    = products;                            //Pass products array to form
                DialogResult resultProducts = addProductForm.ShowDialog();
                if (resultProducts == DialogResult.OK)                            //If add products success
                {
                    product = addProductForm.product;
                    products.Add(product); //Update array
                    this.displayAll();     //Refresh all controls
                }

                break;

            case "tabSuppliers":
                Suppliers             supplier;                                      //Instantiate new supplier
                frmAddModifySuppliers addSupplierForm = new frmAddModifySuppliers(); //Instantiate new add supplier form
                addSupplierForm.addSupplier = true;                                  //set add mode to true
                DialogResult resultSupplier = addSupplierForm.ShowDialog();
                if (resultSupplier == DialogResult.OK)                               //If added
                {
                    supplier = addSupplierForm.supplier;
                    suppliers.Add(supplier); //update array
                    this.displayAll();       //refresh Controls
                }

                break;

            case "tabPackageProductSupplier":

                Package             packageHome;                                    //Instantite new package
                frmAddModifyPackage addPackageFormHome = new frmAddModifyPackage(); //Instantiate add package form
                addPackageFormHome.addPackage = true;                               //Set to add mode
                DialogResult resultPackageHome = addPackageFormHome.ShowDialog();
                if (resultPackageHome == DialogResult.OK)                           //If done?
                {
                    packageHome = addPackageFormHome.package;                       //collect response from add
                    packages.Add(packageHome);                                      //Update array

                    int rowIndex = dgPackageProductSuppliers.RowCount;              //Hold state
                    this.displayAll(rowIndex, 0);                                   //refresh controls keeping state
                }
                break;
            }
        }