function
This commit is contained in:
@@ -18,6 +18,7 @@ namespace CarManagerV2
|
||||
|
||||
public Car(int id, string make, string model, int year, string color, int mileage, decimal price)
|
||||
{
|
||||
this.id = id;
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
this.year = year;
|
||||
@@ -50,6 +51,7 @@ namespace CarManagerV2
|
||||
Console.WriteLine(csv);
|
||||
string[] parts = csv.Split(';');
|
||||
Console.WriteLine(parts.Length);
|
||||
Console.WriteLine($"Fetched ID: {parts[0]}");
|
||||
return new Car(int.Parse(parts[0]), parts[1], parts[2], int.Parse(parts[3]), parts[4], int.Parse(parts[5]), decimal.Parse(parts[6]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,9 +33,23 @@ namespace CarManagerV2
|
||||
{
|
||||
InitializeComponent();
|
||||
this.Cursor = Cursors.Hand;
|
||||
|
||||
foreach (Control ctrl in this.Controls)
|
||||
{
|
||||
ctrl.Click += ForwardClick;
|
||||
foreach (Control inner in ctrl.Controls) // In case you have nested controls
|
||||
inner.Click += ForwardClick;
|
||||
}
|
||||
|
||||
this.Click += (s, e) => this.OnCardClicked();
|
||||
}
|
||||
|
||||
private void ForwardClick(object sender, EventArgs e)
|
||||
{
|
||||
// Raise your CardClicked event no matter what got clicked
|
||||
CardClicked?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public event EventHandler CardClicked;
|
||||
private void OnCardClicked()
|
||||
{
|
||||
|
||||
@@ -37,38 +37,41 @@ namespace CarManagerV2
|
||||
|
||||
private void tbxMake_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
this.car.Make = tbxMake.Text;
|
||||
car.Make = tbxMake.Text;
|
||||
}
|
||||
|
||||
private void tbxModel_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
this.car.Model = tbxModel.Text;
|
||||
car.Model = tbxModel.Text;
|
||||
}
|
||||
|
||||
private void nudYear_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
this.car.Year = (int)nudYear.Value;
|
||||
car.Year = (int)nudYear.Value;
|
||||
Console.WriteLine(car.Year);
|
||||
}
|
||||
|
||||
private void tbxColor_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
this.car.Color = tbxColor.Text;
|
||||
car.Color = tbxColor.Text;
|
||||
}
|
||||
|
||||
private void nudMileage_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
this.car.Mileage = (int)nudMileage.Value;
|
||||
car.Mileage = (int)nudMileage.Value;
|
||||
}
|
||||
|
||||
private void nudPrice_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
this.car.Price = nudPrice.Value;
|
||||
car.Price = nudPrice.Value;
|
||||
}
|
||||
|
||||
private void btnSave_Click(object sender, EventArgs e)
|
||||
{
|
||||
// save car to cars.csv and close form
|
||||
StateManager.updateCar(this.car);
|
||||
Console.WriteLine("Saving car: " + car.Id);
|
||||
Console.WriteLine("Car year: " + car.Year);
|
||||
StateManager.updateCar(car);
|
||||
this.Close();
|
||||
}
|
||||
|
||||
@@ -78,7 +81,7 @@ namespace CarManagerV2
|
||||
var result = MessageBox.Show("Are you sure you want to delete this car?", "Delete Car", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
|
||||
if (result == DialogResult.Yes)
|
||||
{
|
||||
StateManager.removeCar(this.car);
|
||||
StateManager.removeCar(car);
|
||||
this.Close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,15 +28,17 @@ namespace CarManagerV2
|
||||
foreach (Car car in cars)
|
||||
{
|
||||
CarCard card = new CarCard();
|
||||
card.CarName = $"{car.Make} {car.Model}";
|
||||
card.CarName = $"{car.Make} {car.Model} ({car.Id})";
|
||||
card.CarDetails = $"{car.Year}, {car.Mileage} km, ${car.Price}";
|
||||
card.CarImage = ImageManager.GetImage(car);
|
||||
card.CardClicked += (s, e) =>
|
||||
{
|
||||
Console.WriteLine($"Card clicked: {car.Id}");
|
||||
CarDetailsForm detailsForm = new CarDetailsForm(car);
|
||||
detailsForm.FormClosed += (s2, e2) =>
|
||||
{
|
||||
// refresh cars
|
||||
Console.WriteLine("Refreshing cars...");
|
||||
cars = SafeManager.readCars("cars.csv");
|
||||
refreshCars(cars);
|
||||
};
|
||||
|
||||
@@ -40,8 +40,10 @@ namespace CarManagerV2
|
||||
Car existingCar = getCarById(car.Id);
|
||||
if (existingCar != null)
|
||||
{
|
||||
cars.Remove(existingCar);
|
||||
cars.Add(car);
|
||||
int index = cars.IndexOf(existingCar);
|
||||
cars[index] = car;
|
||||
Console.WriteLine("Updated car: " + existingCar.Id);
|
||||
SafeManager.saveCars("cars.csv", cars);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user