private void btnSave_Click(object sender, EventArgs e)
        {
            Shaft s = new Shaft();
            s.Name = txtName.Text;
            s.Diameter = Convert.ToDouble(numDiameter.Value);
            s.KeyCutWidth = Convert.ToDouble(numKeyCutWidth.Value);
            s.KeyCutHeigth = Convert.ToDouble(numKeyCutHeight.Value);
            s.KeyCutLength = Convert.ToDouble(numKeyCutLength.Value);
            s.Material = material;

            if (s.Name != "" && s.Material != null)
            {
                Database1DataSet1TableAdapters.Shaft1TableAdapter sta = new Database1DataSet1TableAdapters.Shaft1TableAdapter();
                sta.Insert(s.Name, s.Diameter, s.Material.ID, s.KeyCutWidth, s.KeyCutHeigth, s.KeyCutLength);
                s.ID = (int)sta.getID(s.Name, s.Diameter, s.Material.ID, s.KeyCutWidth, s.KeyCutHeigth, s.KeyCutLength);
                Program.shaftList.Add(s);
                DialogResult = DialogResult.OK;
            }
            else
            {
                if (s.Name == "")
                    MessageBox.Show("You have to add a Name to your shaft", "Impossible to save the gear", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                    MessageBox.Show("You have to add a Material to your shaft", "Impossible to save the gear", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
        }
        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 btnRemove_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("You may delete Gears and Gear Boxes associated. Do you want to continue ?", "Caution", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
            {
                if (listShaft.SelectedItems.Count > 0)
                {
                    Shaft shaftToDelete = new Shaft();
                    shaftToDelete = (Shaft)listShaft.SelectedItems[0];

                    Program.shaftList.Remove(shaftToDelete);
                    Database1DataSet1TableAdapters.Shaft1TableAdapter sta = new Database1DataSet1TableAdapters.Shaft1TableAdapter();
                    Database1DataSet1TableAdapters.GearsTableAdapter gta = new Database1DataSet1TableAdapters.GearsTableAdapter();
                    Database1DataSet1TableAdapters.GearBoxTableAdapter gbta = new Database1DataSet1TableAdapters.GearBoxTableAdapter();

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

                        gta.DeleteShaftQuery(shaftToDelete.ID);
                        sta.Delete(shaftToDelete.ID, shaftToDelete.Name, shaftToDelete.Diameter, shaftToDelete.Material.ID, shaftToDelete.KeyCutWidth, shaftToDelete.KeyCutHeigth, shaftToDelete.KeyCutLength);
                        Util.refreshGearBoxList();
                    }
                    catch (Exception ex) { }
                }
                refreshList();
                treeViewShaft.Nodes.Clear();
                propertyGridShaft.SelectedObject = null;
                btnRemove.Enabled = false;
                btnEdit.Enabled = false;
                btnSelect.Enabled = false;
            }
        }
        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();
        }
        private void btnValidate_Click(object sender, EventArgs e)
        {
            Database1DataSet1TableAdapters.Shaft1TableAdapter sta = new Database1DataSet1TableAdapters.Shaft1TableAdapter();
            sta.Insert(inputGear.Shaft.Name, inputGear.Shaft.Diameter, inputGear.Shaft.Material.ID, inputGear.Shaft.KeyCutWidth, inputGear.Shaft.KeyCutHeigth, inputGear.Shaft.KeyCutLength);
            sta.Insert(outputGear.Shaft.Name, outputGear.Shaft.Diameter, outputGear.Shaft.Material.ID, outputGear.Shaft.KeyCutWidth, outputGear.Shaft.KeyCutHeigth, outputGear.Shaft.KeyCutLength);

            inputGear.Shaft.ID = (int)sta.getID(inputGear.Shaft.Name, inputGear.Shaft.Diameter, inputGear.Shaft.Material.ID, inputGear.Shaft.KeyCutWidth, inputGear.Shaft.KeyCutHeigth, inputGear.Shaft.KeyCutLength);
            outputGear.Shaft.ID = (int)sta.getID(outputGear.Shaft.Name, outputGear.Shaft.Diameter, outputGear.Shaft.Material.ID, outputGear.Shaft.KeyCutWidth, outputGear.Shaft.KeyCutHeigth, outputGear.Shaft.KeyCutLength);

            Database1DataSet1TableAdapters.GearsTableAdapter gta = new Database1DataSet1TableAdapters.GearsTableAdapter();
            gta.Insert(inputGear.Name, inputGear.Alpha,inputGear.GearModule, inputGear.Material.ID, inputGear.Torque, inputGear.Shaft.ID, inputGear.WantedRadius, inputGear.Z, inputGear.Phi, inputGear.R_base, inputGear.R_foot, inputGear.R_pitch, inputGear.R_top);
            gta.Insert(outputGear.Name, outputGear.Alpha, outputGear.GearModule, outputGear.Material.ID, outputGear.Torque, outputGear.Shaft.ID, outputGear.WantedRadius, outputGear.Z, outputGear.Phi, outputGear.R_base, outputGear.R_foot, outputGear.R_pitch, outputGear.R_top);

            inputGear.ID = (int)gta.getID(inputGear.Name, inputGear.Alpha, inputGear.GearModule, inputGear.Material.ID, inputGear.Torque, inputGear.Shaft.ID, inputGear.WantedRadius, inputGear.Z, inputGear.Phi, inputGear.R_base, inputGear.R_foot, inputGear.R_pitch, inputGear.R_top);
            outputGear.ID = (int)gta.getID(outputGear.Name, outputGear.Alpha, outputGear.GearModule, outputGear.Material.ID, outputGear.Torque, outputGear.Shaft.ID, outputGear.WantedRadius, outputGear.Z, outputGear.Phi, outputGear.R_base, outputGear.R_foot, outputGear.R_pitch, outputGear.R_top);

            Database1DataSet1TableAdapters.GearBoxTableAdapter gbta = new Database1DataSet1TableAdapters.GearBoxTableAdapter();
            gbta.Insert(gearBox.Name, gearBox.Alpha, gearBox.AxesDistance, gearBox.GearModule, gearBox.InputTorque, gearBox.OutputTorque, gearBox.TorqueRatio, gearBox.InputGear.ID, gearBox.OutputGear.ID);

            gearBox.ID = (int)gbta.getID(gearBox.Name, gearBox.Alpha, gearBox.AxesDistance, gearBox.GearModule, gearBox.InputTorque, gearBox.OutputTorque, gearBox.TorqueRatio, gearBox.InputGear.ID, gearBox.OutputGear.ID);

            Program.gearBoxList.Add(gearBox);
            Program.gearList.Add(inputGear);
            Program.gearList.Add(outputGear);
            Program.shaftList.Add(inputGear.Shaft);
            Program.shaftList.Add(outputGear.Shaft);

            tsStatusLabel.Text = "Gear Box saved !";
        }