private void btnRemove_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("You may delete the Shafts, Gears and Gear Boxes associated. Do you want to continue ?", "Caution", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
            {
                if (listMaterial.SelectedItems.Count > 0)
                {
                    Material materialToDelete = new Material();
                    materialToDelete = (Material)listMaterial.SelectedItems[0];

                    Program.materialList.Remove(materialToDelete);
                    Database1DataSet1TableAdapters.MaterialsTableAdapter mta = new Database1DataSet1TableAdapters.MaterialsTableAdapter();
                    Database1DataSet1TableAdapters.Shaft1TableAdapter sta = new Database1DataSet1TableAdapters.Shaft1TableAdapter();
                    Database1DataSet1TableAdapters.GearsTableAdapter gta = new Database1DataSet1TableAdapters.GearsTableAdapter();
                    Database1DataSet1TableAdapters.GearBoxTableAdapter gbta = new Database1DataSet1TableAdapters.GearBoxTableAdapter();

                    List<int> shaftIds = Util.listOfShaftIds(materialToDelete.ID, sta);
                    List<int> gearIds = Util.listOfGearIds(materialToDelete.ID, shaftIds, gta);
                    try
                    {
                        foreach (int g in gearIds)
                        {
                            gbta.DeleteInputGearQuery(g);
                            gbta.DeleteOutputGearQuery(g);
                            Gear gear = Util.getGearById(g);
                            Program.gearList.Remove(gear);
                        }

                        foreach (int s in shaftIds)
                        {
                            gta.DeleteShaftQuery(s);
                            Shaft shaft = Util.getShaftById(s);
                            Program.shaftList.Remove(shaft);
                        }
                        sta.DeleteQuery(materialToDelete.ID);
                        mta.Delete(materialToDelete.ID, materialToDelete.Name, materialToDelete.youngsModulus, materialToDelete.Density, materialToDelete.yieldStress);
                        Util.refreshGearBoxList();
                    }
                    catch (Exception ex) { }

                }
                refreshList();
                propertyGridMaterial.SelectedObject = null;
                btnRemove.Enabled = false;
                btnEdit.Enabled = false;
                btnSelect.Enabled = false;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            Material m = new Material();
            m.Name = txtName.Text;
            m.Density = Convert.ToDouble(numDensity.Value);
            m.yieldStress = Convert.ToDouble(numYieldStress.Value);
            m.youngsModulus = Convert.ToDouble(numYoungModulus.Value);

            if (m.Name != "")
            {
                Database1DataSet1TableAdapters.MaterialsTableAdapter mta = new Database1DataSet1TableAdapters.MaterialsTableAdapter();
                mta.Insert(m.Name, m.youngsModulus, m.Density, m.yieldStress);
                m.ID = (int)mta.getID(m.Name, m.youngsModulus, m.Density, m.yieldStress);
                Program.materialList.Add(m);
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("You have to add a Name to your Material", "Impossible to save the gear", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void GearBoxGenerator_Load(object sender, EventArgs e)
        {
            // Get the Materials
            Database1DataSet1TableAdapters.MaterialsTableAdapter mta = new Database1DataSet1TableAdapters.MaterialsTableAdapter();
            Database1DataSet1.MaterialsDataTable MaterialsFromDB = new Database1DataSet1.MaterialsDataTable();
            mta.Fill(MaterialsFromDB);

            foreach(Database1DataSet1.MaterialsRow row in MaterialsFromDB.Rows){
                Material mat = new Material();
                mat.ID = row.ID;
                mat.Name = row.MaterialName;
                mat.Density = row.Density;
                mat.yieldStress = row.YieldStress;
                mat.youngsModulus = row.YoungsModulus;

                Program.materialList.Add(mat);

            }

            // Get the Shafts
            Database1DataSet1TableAdapters.Shaft1TableAdapter sta = new Database1DataSet1TableAdapters.Shaft1TableAdapter();
            Database1DataSet1.Shaft1DataTable ShaftsFromDB = new Database1DataSet1.Shaft1DataTable();
            sta.Fill(ShaftsFromDB);

            foreach (Database1DataSet1.Shaft1Row row in ShaftsFromDB.Rows) {
                Shaft shaft = new Shaft();
                shaft.ID = row.ID;
                shaft.Name = row.ShaftName;
                shaft.Diameter = row.Diameter;
                shaft.KeyCutHeigth = row.KeyCutHeight;
                shaft.KeyCutLength = row.KeyCutLenght;
                shaft.KeyCutWidth = row.KeyCutWidth;
                shaft.Material = Util.getMaterialById(row.MaterialID);

                Program.shaftList.Add(shaft);
            }

            Database1DataSet1TableAdapters.GearsTableAdapter gta = new Database1DataSet1TableAdapters.GearsTableAdapter();
            Database1DataSet1.GearsDataTable GearsFromDB = new Database1DataSet1.GearsDataTable();
            gta.Fill(GearsFromDB);

            foreach (Database1DataSet1.GearsRow row in GearsFromDB.Rows)
            {
                Gear gear = new Gear();
                gear.ID = row.ID;
                gear.Alpha = row.Alpha;
                gear.GearModule = row.GearModule;
                gear.Torque = row.Torque;
                gear.WantedRadius = row.WantedRadius;
                gear.Name = row.GearName;
                gear.Z = row.Z;
                gear.Phi = row.Phi;
                gear.R_base = row.RBase;
                gear.R_foot = row.RFoot;
                gear.R_pitch = row.RPitch;
                gear.R_top = row.RTop;
                gear.Material = Util.getMaterialById(row.MaterialID);
                gear.Shaft = Util.getShaftById(row.ShaftID);

                Program.gearList.Add(gear);
            }

            Util.refreshGearBoxList();
        }