feat: Better car creation

This commit is contained in:
2026-02-24 08:57:47 +01:00
parent 4404ac3c7b
commit f6b70fa387
7 changed files with 71 additions and 20 deletions

View File

@@ -43,20 +43,20 @@
this.btnSave = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.lblID = new System.Windows.Forms.Label();
this.nudYear = new System.Windows.Forms.NumericUpDown();
this.tbxColor = new System.Windows.Forms.TextBox();
this.nudMileage = new System.Windows.Forms.NumericUpDown();
this.nudPrice = new System.Windows.Forms.NumericUpDown();
this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
this.nudYear = new System.Windows.Forms.NumericUpDown();
this.lblAge = new System.Windows.Forms.Label();
this.tbxAge = new System.Windows.Forms.TextBox();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pbxCarImage)).BeginInit();
this.flowLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nudYear)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudMileage)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudPrice)).BeginInit();
this.tableLayoutPanel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nudYear)).BeginInit();
this.SuspendLayout();
//
// tableLayoutPanel1
@@ -238,19 +238,6 @@
this.lblID.TabIndex = 2;
this.lblID.Text = "ID";
//
// nudYear
//
this.nudYear.Location = new System.Drawing.Point(3, 3);
this.nudYear.Maximum = new decimal(new int[] {
3000,
0,
0,
0});
this.nudYear.Name = "nudYear";
this.nudYear.Size = new System.Drawing.Size(120, 22);
this.nudYear.TabIndex = 3;
this.nudYear.ValueChanged += new System.EventHandler(this.nudYear_ValueChanged);
//
// tbxColor
//
this.tbxColor.Dock = System.Windows.Forms.DockStyle.Fill;
@@ -307,6 +294,19 @@
this.tableLayoutPanel2.Size = new System.Drawing.Size(473, 24);
this.tableLayoutPanel2.TabIndex = 10;
//
// nudYear
//
this.nudYear.Location = new System.Drawing.Point(3, 3);
this.nudYear.Maximum = new decimal(new int[] {
3000,
0,
0,
0});
this.nudYear.Name = "nudYear";
this.nudYear.Size = new System.Drawing.Size(120, 22);
this.nudYear.TabIndex = 3;
this.nudYear.ValueChanged += new System.EventHandler(this.nudYear_ValueChanged);
//
// lblAge
//
this.lblAge.AutoSize = true;
@@ -343,11 +343,11 @@
((System.ComponentModel.ISupportInitialize)(this.pbxCarImage)).EndInit();
this.flowLayoutPanel1.ResumeLayout(false);
this.flowLayoutPanel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.nudYear)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudMileage)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudPrice)).EndInit();
this.tableLayoutPanel2.ResumeLayout(false);
this.tableLayoutPanel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.nudYear)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

View File

@@ -7,7 +7,7 @@ namespace CarManagerV3
public partial class CarDetailsForm : Form
{
Car car;
public Car car;
public CarDetailsForm(Car car)
{
@@ -21,7 +21,14 @@ namespace CarManagerV3
nudPrice.Value = car.Price;
tbxAge.Text = car.AgeString;
pbxCarImage.Image = ImageManager.GetImage(car);
lblID.Text = $"ID: {car.Id}";
if (car.Id == 0)
{
lblID.Text = "New Car";
}
else
{
lblID.Text = $"ID: {car.Id}";
}
}
/// <summary>
@@ -118,7 +125,12 @@ namespace CarManagerV3
msgbox.Show();
await Task.Run(() =>
{
StateManager.UpdateCar(car);
if(car.Id == 0) {
car = StateManager.CreateCar(car.Make, car.Model, car.Year, car.Color, car.Mileage, car.Price);
}
else {
StateManager.UpdateCar(car);
}
Console.WriteLine("Saved car: " + car.Id);
});
Console.WriteLine("Car saved. " + car.Id);
@@ -133,6 +145,12 @@ namespace CarManagerV3
private void btnDelete_Click(object sender, EventArgs e)
{
if(car.Id == 0)
{
//just close form if car is not saved yet
this.Close();
return;
}
//are you sure?
var result = MessageBox.Show("Are you sure you want to delete this car?", "Delete Car", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)

View File

@@ -135,7 +135,7 @@ namespace CarManagerV3
private void btnNewCar_Click(object sender, EventArgs e)
{
Car foocar = StateManager.CreateCar("New", "Car", 2020, "White", 0, 20000);
Car foocar = new Car(0, "New", "Car", 2020, "White", 0, 20000);
CarDetailsForm detailsForm = new CarDetailsForm(foocar);
detailsForm.FormClosed += (s2, e2) =>
{