예제 #1
0
        private void addTruckButton_Click(object sender, EventArgs e)
        {
            TruckForm        truckForm  = new TruckForm();
            List <TypeTruck> typeTrucks = db.TypeTrucks.ToList();

            truckForm.comboBox1.DataSource    = typeTrucks;
            truckForm.comboBox1.ValueMember   = "Id";
            truckForm.comboBox1.DisplayMember = "Name";
            List <Mark> markTruck = db.Marks.ToList();

            truckForm.comboBox2.DataSource    = markTruck;
            truckForm.comboBox1.ValueMember   = "Id";
            truckForm.comboBox1.DisplayMember = "Name";
            DialogResult result = truckForm.ShowDialog(this);

            if (result == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                try
                {
                    Truck truck = new Truck();
                    truck.Model       = truckForm.textBox1.Text;
                    truck.Power       = (int)truckForm.numericUpDown1.Value;
                    truck.Consumption = (int)truckForm.numericUpDown2.Value;
                    truck.Mileage     = (int)truckForm.numericUpDown3.Value;
                    truck.Photo       = ConvertFiletoByte(truckForm.pictureBox1.ImageLocation);
                    truck.TypeTruck   = (TypeTruck)truckForm.comboBox1.SelectedItem;
                    truck.Mark        = (Mark)truckForm.comboBox2.SelectedItem;
                    db.Trucks.Add(truck);
                    db.SaveChanges();
                    PlaySound(Application.StartupPath + "\\exclamationtone.wav", 0, 1);
                    MessageBox.Show("Новый грузовик добавлен");
                }
                catch
                {
                    PlaySound(Application.StartupPath + "\\errortone.wav", 0, 1);
                    MessageBox.Show("Не получилось добавить новый объект");
                }
            }
        }
예제 #2
0
        private void chTruckButton_Click(object sender, EventArgs e)
        {
            int  index     = dataGridView3.SelectedRows[0].Index;
            int  id        = 0;
            bool converted = Int32.TryParse(dataGridView3[0, index].Value.ToString(), out id);

            if (converted == false)
            {
                return;
            }
            Truck     truck     = db.Trucks.Find(id);
            TruckForm truckForm = new TruckForm();

            truckForm.textBox1.Text        = truck.Model;
            truckForm.numericUpDown1.Value = truck.Power;
            truckForm.numericUpDown2.Value = truck.Consumption;
            truckForm.numericUpDown3.Value = truck.Mileage;
            truckForm.pictureBox1.Image    = ConvertBytetoImage(truck.Photo);
            List <TypeTruck> typeTrucks = db.TypeTrucks.ToList();

            truckForm.comboBox1.DataSource    = typeTrucks;
            truckForm.comboBox1.ValueMember   = "Id";
            truckForm.comboBox1.DisplayMember = "Name";
            List <Mark> marks = db.Marks.ToList();

            truckForm.comboBox2.DataSource    = marks;
            truckForm.comboBox2.ValueMember   = "Id";
            truckForm.comboBox2.DisplayMember = "Name";
            if (truck.TypeTruck != null)
            {
                truckForm.comboBox1.SelectedValue = truck.TypeTruck.Id;
            }
            if (truck.Mark != null)
            {
                truckForm.comboBox2.SelectedValue = truck.Mark.Id;
            }
            DialogResult result = truckForm.ShowDialog(this);

            if (result == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                PlaySound(Application.StartupPath + "\\strarttone.wav", 0, 1);
                DialogResult dialogResult = MessageBox.Show("Желаете поменять изображение для этого объекта? " +
                                                            "Если вы не выбрали новое изображение для него и нажали Да, то старое изображение пропадет",
                                                            "Сменить изображение?", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    truck.Model           = truckForm.textBox1.Text;
                    truck.Power           = (int)truckForm.numericUpDown1.Value;
                    truck.Consumption     = (int)truckForm.numericUpDown2.Value;
                    truck.Mileage         = (int)truckForm.numericUpDown3.Value;
                    truck.TypeTruck       = (TypeTruck)truckForm.comboBox1.SelectedItem;
                    truck.Mark            = (Mark)truckForm.comboBox2.SelectedItem;
                    truck.Photo           = ConvertFiletoByte(truckForm.pictureBox1.ImageLocation);
                    db.Entry(truck).State = EntityState.Modified;
                    db.SaveChanges();
                    MessageBox.Show("Объект обновлен");
                }
                else if (dialogResult == DialogResult.No)
                {
                    truck.Model           = truckForm.textBox1.Text;
                    truck.Power           = (int)truckForm.numericUpDown1.Value;
                    truck.Consumption     = (int)truckForm.numericUpDown2.Value;
                    truck.Mileage         = (int)truckForm.numericUpDown3.Value;
                    truck.TypeTruck       = (TypeTruck)truckForm.comboBox1.SelectedItem;
                    truck.Mark            = (Mark)truckForm.comboBox2.SelectedItem;
                    db.Entry(truck).State = EntityState.Modified;
                    db.SaveChanges();
                    MessageBox.Show("Объект обновлен");
                }
            }
        }