//If add new supplier radio button is changed
 private void rdAddSuppfromPro_CheckedChanged(object sender, EventArgs e)
 {
     if (rdAddSuppfromPro.Checked)//open add supplier forms
     {
         Suppliers             supplier;
         frmAddModifySuppliers addSupplierForm = new frmAddModifySuppliers();
         addSupplierForm.addSupplier = true;
         DialogResult resultSupplier = addSupplierForm.ShowDialog();
         if (resultSupplier == DialogResult.OK)
         {
             supplier = addSupplierForm.supplier;
             suppliers.Add(supplier);
             loadProdSuppliers();
             cmbSuppliers.SelectedIndex = suppliers.Count - 1; //select the last added supplier on default
             cmbSuppliers.Enabled       = false;
         }
     }
     else
     {
         //Else allow user to select from combobox
         cmbSuppliers.Enabled = true;
     }
 }
        /// <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;
            }
        }