Beispiel #1
0
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            Car car;

            try
            {
                var    brand            = !String.IsNullOrEmpty(BarndTextBox.Text) ? BarndTextBox.Text : "";
                var    model            = !String.IsNullOrEmpty(ModelTextBox.Text) ? ModelTextBox.Text : "";
                var    totalCost        = Convert.ToInt32(!String.IsNullOrEmpty(TotalCostTextBox.Text) ? TotalCostTextBox.Text : null);
                var    yearOfProduction = Convert.ToInt32(!String.IsNullOrEmpty(YearOfProductionTextBox.Text) ? YearOfProductionTextBox.Text : null);
                var    mileage          = Convert.ToInt32(!String.IsNullOrEmpty(MileageTextBox.Text) ? MileageTextBox.Text : null);
                string fuel             = !String.IsNullOrEmpty(FuelTextBox.Text) ? FuelTextBox.Text : "";
                string carType          = !String.IsNullOrEmpty(CarTypeTextBox.Text) ? CarTypeTextBox.Text : "";
                int    seats            = Convert.ToInt32(!String.IsNullOrEmpty(SeatsTextBox.Text) ? SeatsTextBox.Text : null);
                string color            = !String.IsNullOrEmpty(ColorTextBox.Text) ? ColorTextBox.Text : "";
                string carStatus        = !String.IsNullOrEmpty(CarStatusTextBox.Text) ? CarStatusTextBox.Text : "";

                car = new Car(brand, model, totalCost, yearOfProduction, mileage, fuel, carType, seats, color, carStatus);
            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid data format.", "Added Car");
                return;
            }

            if (car.IsEmpty())
            {
                var result = MessageBox.Show("Are you want to add an empty record to the database ", "Empty record!", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                switch (result)
                {
                case MessageBoxResult.Yes:
                    break;

                case MessageBoxResult.No:
                    return;
                }
            }

            viewModel.AddCar(car);
            ClearTextBoxes();
            MessageBox.Show("Record has been added.", "Added Car");
        }