Пример #1
0
        private void Add_Click(object sender, EventArgs e)
        {
            // Validation rules
            if (dataGridView.Rows.Count == 0)
            {
                MessageBox.Show("Моля заредете първо данните с бутона \"Прегледай\".");
                Clear();
                return;
            }

            if (chooseList.Text == string.Empty)
            {
                MessageBox.Show("Моля изберете първо тип двигател за добавяне.");
                return;
            }
            else if (string.IsNullOrEmpty(PriceRedTextbox.Text) || string.IsNullOrEmpty(PriceGreenTextbox.Text) ||
                     string.IsNullOrEmpty(CapacityTextbox.Text) || string.IsNullOrEmpty(VoltageTextbox.Text) ||
                     string.IsNullOrEmpty(RpmTextbox.Text) || string.IsNullOrEmpty(AmperageTextbox.Text))
            {
                MessageBox.Show("Моля въведете всички полета.");
                Clear();
                return;
            }

            string idAsString             = dataGridView.CurrentRow.Cells["Id"].Value.ToString();
            string priceRedCardAsString   = PriceRedTextbox.Text.Trim();
            string priceGreenCardAsString = PriceGreenTextbox.Text.Trim();
            string capacityAsString       = CapacityTextbox.Text.Trim();
            string voltageAsString        = VoltageTextbox.Text.Trim();
            string rpmAsString            = RpmTextbox.Text.Trim();
            string amperageAsString       = AmperageTextbox.Text.Trim();

            string patternForValidation = @"^[0-9]*$";

            if (!Regex.IsMatch(idAsString, patternForValidation) || !Regex.IsMatch(priceRedCardAsString, patternForValidation) ||
                !Regex.IsMatch(priceGreenCardAsString, patternForValidation) ||
                !Regex.IsMatch(capacityAsString, patternForValidation) ||
                !Regex.IsMatch(voltageAsString, patternForValidation) || !Regex.IsMatch(rpmAsString, patternForValidation) ||
                !Regex.IsMatch(amperageAsString, patternForValidation))
            {
                MessageBox.Show("Невалидни данни за вход. Моля въведете само цифри.");
                Clear();
                return;
            }

            // Parsing string to data variables
            int     id             = Convert.ToInt32(idAsString);
            decimal priceRedCard   = decimal.Parse(priceRedCardAsString);
            decimal priceGreenCard = decimal.Parse(priceGreenCardAsString);
            double  capacity       = double.Parse(capacityAsString);
            int     voltage        = int.Parse(voltageAsString);
            int     rpm            = int.Parse(rpmAsString);
            int     amperage       = int.Parse(amperageAsString);

            // Main logic
            if (chooseList.Text == "Asynchrone")
            {
                AcEngine acEngine = new AcEngine(id, priceRedCard, priceGreenCard, capacity, voltage, rpm, amperage);

                using (EngineDbContext db = new EngineDbContext())
                {
                    // Update information
                    if (AddBtn.Text == "Ъпдейт")
                    {
                        db.Entry(acEngine).State = EntityState.Modified;
                    }
                    else // Add information
                    {
                        db.AcEngines.Add(acEngine);
                        Counter(true);
                    }

                    db.SaveChanges();
                }

                Clear();
                ShowDataGridView();
                MessageBox.Show("Submitted successfully.");
            }
            else if (chooseList.Text == "Dc")
            {
                DcEngine dcEngine = new DcEngine(id, priceRedCard, priceGreenCard, capacity, voltage, rpm, amperage);

                using (EngineDbContext db = new EngineDbContext())
                {
                    // Update information
                    if (AddBtn.Text == "Ъпдейт")
                    {
                        db.Entry(dcEngine).State = EntityState.Modified;
                    }
                    else // Add information
                    {
                        db.DcEngines.Add(dcEngine);
                        Counter(true);
                    }
                    db.SaveChanges();
                }

                Clear();
                ShowDataGridView();
                MessageBox.Show("Submitted successfully.");
            }
            else if (chooseList.Text == "Generator")
            {
                Generator generator = new Generator(id, priceRedCard, priceGreenCard, capacity, voltage, rpm, amperage);

                using (EngineDbContext db = new EngineDbContext())
                {
                    // Update information
                    if (AddBtn.Text == "Ъпдейт")
                    {
                        db.Entry(generator).State = EntityState.Modified;
                    }
                    else // Add information
                    {
                        db.Generators.Add(generator);
                        Counter(true);
                    }
                    db.SaveChanges();
                }

                Clear();
                ShowDataGridView();
                MessageBox.Show("Submitted successfully.");
            }
        }
Пример #2
0
        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            // Validation message
            bool confirmation = MessageBox.Show("Сигурни ли сте, че искате да изтриете данните?", "Изтриване", MessageBoxButtons.YesNo)
                                == DialogResult.Yes;

            // Main logic
            if (confirmation)
            {
                using (EngineDbContext db = new EngineDbContext())
                {
                    if (chooseList.Text == "Asynchrone")
                    {
                        AcEngine acEngineToDelete = new AcEngine
                        {
                            Id = Convert.ToInt32(dataGridView.CurrentRow.Cells["Id"].Value)
                        };

                        var entry = db.Entry(acEngineToDelete);
                        if (entry.State == EntityState.Detached)
                        {
                            db.AcEngines.Attach(acEngineToDelete);
                        }

                        db.AcEngines.Remove(acEngineToDelete);
                        Counter(false);
                        db.SaveChanges();
                        ShowDataGridView();
                        Clear();
                        MessageBox.Show("Deleted successfully.");
                    }
                    else if (chooseList.Text == "Dc")
                    {
                        DcEngine dcEngineToDelete = new DcEngine
                        {
                            Id = Convert.ToInt32(dataGridView.CurrentRow.Cells["Id"].Value)
                        };

                        var entry = db.Entry(dcEngineToDelete);
                        if (entry.State == EntityState.Detached)
                        {
                            db.DcEngines.Attach(dcEngineToDelete);
                        }

                        db.DcEngines.Remove(dcEngineToDelete);
                        Counter(false);
                        db.SaveChanges();
                        ShowDataGridView();
                        Clear();
                        MessageBox.Show("Deleted successfully.");
                    }
                    else if (chooseList.Text == "Generator")
                    {
                        Generator generatorToDelete = new Generator
                        {
                            Id = Convert.ToInt32(dataGridView.CurrentRow.Cells["Id"].Value)
                        };

                        var entry = db.Entry(generatorToDelete);
                        if (entry.State == EntityState.Detached)
                        {
                            db.Generators.Attach(generatorToDelete);
                        }

                        db.Generators.Remove(generatorToDelete);
                        Counter(false);
                        db.SaveChanges();
                        ShowDataGridView();
                        Clear();
                        MessageBox.Show("Deleted successfully.");
                    }
                }
            }
        }
        private void RotateBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(IdTextbox.Text) || string.IsNullOrEmpty(VoltageAmount.Text))
            {
                MessageBox.Show("Моля въведете полето/полетата.");
                Clear();
            }
            else
            {
                string voltageAmount        = VoltageAmount.Text.Trim();
                string id                   = IdTextbox.Text.Trim();
                string patternForValidation = @"^[0-9]*$";

                // Validation
                if (!Regex.IsMatch(voltageAmount, patternForValidation) || !Regex.IsMatch(id, patternForValidation))
                {
                    MessageBox.Show("Моля въведете само цифри.");
                    Clear();
                    return;
                }

                // Main logic
                int voltageFromUser = int.Parse(voltageAmount);
                int engineId        = int.Parse(id);

                if (chooseList.Text == "Asynchrone")
                {
                    AcEngine acEngine = new AcEngine();
                    using (EngineDbContext db = new EngineDbContext())
                    {
                        acEngine = db.AcEngines.Where(x => x.Id == engineId).FirstOrDefault();
                        if (acEngine == null)
                        {
                            MessageBox.Show("Не съществува такова ид в базата данни.");
                            Clear();
                            return;
                        }
                    }

                    int maximumAllowedRpm = acEngine.Rpm;
                    acEngine.RotateClockwize(voltageFromUser, maximumAllowedRpm);
                }
                else if (chooseList.Text == "Dc")
                {
                    DcEngine dcEngine = new DcEngine();
                    using (EngineDbContext db = new EngineDbContext())
                    {
                        dcEngine = db.DcEngines.Where(x => x.Id == engineId).FirstOrDefault();
                        if (dcEngine == null)
                        {
                            MessageBox.Show("Не съществува такова ид в базата данни.");
                            Clear();
                            return;
                        }
                    }

                    int maximumAllowedRpm = dcEngine.Rpm;
                    dcEngine.RotateClockwize(voltageFromUser, maximumAllowedRpm);
                }
                else
                {
                    MessageBox.Show("Моля изберете тип двигател.");
                    Clear();
                    return;
                }

                Clear();
            }
        }
Пример #4
0
        private void DataGridView_DoubleClick(object sender, EventArgs e)
        {
            // Validation rule
            if (dataGridView.Rows.Count == 0)
            {
                MessageBox.Show("Моля заредете първо данните с бутона \"Прегледай\".");
                return;
            }

            // Main logic
            if (dataGridView.CurrentRow.Index != -1)
            {
                if (chooseList.Text == "Asynchrone")
                {
                    AcEngine updatedAcEngine = new AcEngine
                    {
                        Id = Convert.ToInt32(dataGridView.CurrentRow.Cells["Id"].Value)
                    };

                    using (EngineDbContext db = new EngineDbContext())
                    {
                        updatedAcEngine = db.AcEngines.Where(x => x.Id == updatedAcEngine.Id).FirstOrDefault();

                        PriceRedTextbox.Text   = updatedAcEngine.PriceRedCard.ToString();
                        PriceGreenTextbox.Text = updatedAcEngine.PriceGreenCard.ToString();
                        CapacityTextbox.Text   = updatedAcEngine.Capacity.ToString();
                        VoltageTextbox.Text    = updatedAcEngine.Voltage.ToString();
                        RpmTextbox.Text        = updatedAcEngine.Rpm.ToString();
                        AmperageTextbox.Text   = updatedAcEngine.Amperage.ToString();
                    }

                    AddBtn.Text       = "Ъпдейт";
                    DeleteBtn.Enabled = true;
                }
                else if (chooseList.Text == "Dc")
                {
                    DcEngine updatedDcEngine = new DcEngine
                    {
                        Id = Convert.ToInt32(dataGridView.CurrentRow.Cells["Id"].Value)
                    };

                    using (EngineDbContext db = new EngineDbContext())
                    {
                        updatedDcEngine = db.DcEngines.Where(x => x.Id == updatedDcEngine.Id).FirstOrDefault();

                        PriceRedTextbox.Text   = updatedDcEngine.PriceRedCard.ToString();
                        PriceGreenTextbox.Text = updatedDcEngine.PriceGreenCard.ToString();
                        CapacityTextbox.Text   = updatedDcEngine.Capacity.ToString();
                        VoltageTextbox.Text    = updatedDcEngine.Voltage.ToString();
                        RpmTextbox.Text        = updatedDcEngine.Rpm.ToString();
                        AmperageTextbox.Text   = updatedDcEngine.Amperage.ToString();
                    }

                    AddBtn.Text       = "Ъпдейт";
                    DeleteBtn.Enabled = true;
                }
                else if (chooseList.Text == "Generator")
                {
                    Generator updatedGenerator = new Generator
                    {
                        Id = Convert.ToInt32(dataGridView.CurrentRow.Cells["Id"].Value)
                    };

                    using (EngineDbContext db = new EngineDbContext())
                    {
                        updatedGenerator = db.Generators.Where(x => x.Id == updatedGenerator.Id).FirstOrDefault();

                        PriceRedTextbox.Text   = updatedGenerator.PriceRedCard.ToString();
                        PriceGreenTextbox.Text = updatedGenerator.PriceGreenCard.ToString();
                        CapacityTextbox.Text   = updatedGenerator.Capacity.ToString();
                        VoltageTextbox.Text    = updatedGenerator.Voltage.ToString();
                        RpmTextbox.Text        = updatedGenerator.Rpm.ToString();
                        AmperageTextbox.Text   = updatedGenerator.Amperage.ToString();
                    }

                    AddBtn.Text       = "Ъпдейт";
                    DeleteBtn.Enabled = true;
                }
            }
        }
Пример #5
0
        private void CalculateBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(IdTextbox.Text))
            {
                MessageBox.Show("Моля въведете полето за ид.");
                Clear();
            }
            else
            {
                string id = IdTextbox.Text.Trim();
                string patternForValidation = @"^[0-9]*$";

                // Validation
                if (!Regex.IsMatch(id, patternForValidation))
                {
                    MessageBox.Show("Моля въведете само цифри.");
                    Clear();
                    return;
                }

                // Main logic
                int engineId = int.Parse(id);

                if (chooseList.Text == "Asynchrone")
                {
                    AcEngine acEngine = new AcEngine();
                    using (EngineDbContext db = new EngineDbContext())
                    {
                        acEngine = db.AcEngines.Where(x => x.Id == engineId).FirstOrDefault();
                        if (acEngine == null)
                        {
                            MessageBox.Show("Не съществува такова ид в базата данни.");
                            Clear();
                            return;
                        }
                    }

                    acEngine.CalculateKPD(acEngine.Capacity, acEngine.Voltage, acEngine.Amperage);
                }
                else if (chooseList.Text == "Dc")
                {
                    DcEngine dcEngine = new DcEngine();
                    using (EngineDbContext db = new EngineDbContext())
                    {
                        dcEngine = db.DcEngines.Where(x => x.Id == engineId).FirstOrDefault();
                        if (dcEngine == null)
                        {
                            MessageBox.Show("Не съществува такова ид в базата данни.");
                            Clear();
                            return;
                        }
                    }

                    dcEngine.CalculateKPD(dcEngine.Capacity, dcEngine.Voltage, dcEngine.Amperage);
                }
                else
                {
                    MessageBox.Show("Моля изберете тип двигател.");
                    Clear();
                    return;
                }

                Clear();
            }
        }