/
AddEditVehicle.cs
110 lines (98 loc) · 3.31 KB
/
AddEditVehicle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarRentalWinforms
{
public partial class AddEditVehicle : Form
{
private bool isEditMode;
public ManageVehicleListing _manageVehicleListing;
//private readonly int _formWidth = 355;
//private readonly int _formHt = 663;
public AddEditVehicle(ManageVehicleListing manageVehicleListing = null)
{
InitializeComponent();
lbFormTitle.Text = "Add a Car";
isEditMode = false;
//this.Width = _formWidth;
//this.Height = _formHt;
_manageVehicleListing = manageVehicleListing;
CarRentalEntities _db = new CarRentalEntities();
}
public AddEditVehicle(TypeOfCar car2Edit, ManageVehicleListing manageVehicleListing = null)
{
InitializeComponent();
lbFormTitle.Text = "Edit a Car";
PopulateFields(car2Edit);
_manageVehicleListing = manageVehicleListing;
isEditMode = true;
}
private void PopulateFields(TypeOfCar car)
{
try
{
if (car != null)
{
txtMake.Text = car.Make;
txtModel.Text = car.Model;
txtVIN.Text = "n/a";
txtYear.Text = car.Year.ToString();
lbID.Text = car.TypeOfCarID.ToString();
if (car.LicensePlateNum != null)
{
txtLicensePlateNum.Text = car.LicensePlateNum;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw new NotImplementedException();
}
}
private void btnSaveChanges_Click(object sender, EventArgs e)
{
CarRentalEntities _db = new CarRentalEntities();
var newCar = new TypeOfCar
{
Make = txtMake.Text,
Model = txtModel.Text,
Year = int.Parse(txtYear.Text),
LicensePlateNum = txtLicensePlateNum.Text
};
if (isEditMode == false)
{
//Add
_db.TypeOfCars.Add(newCar);
}
else
{
// Edit
int id = int.Parse(lbID.Text);
var car = _db.TypeOfCars.Find(id);
if (car != null)
{
car.Make = txtMake.Text;
car.Model = txtModel.Text;
car.Year = int.Parse(txtYear.Text);
car.LicensePlateNum = txtLicensePlateNum.Text;
}
}
_db.SaveChanges();
_manageVehicleListing.PopulateGrid();
// ManageVehicleListing gridPage = new ManageVehicleListing();
// gridPage.PopulateGrid();
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}