feat: set & get validation
This commit is contained in:
@@ -29,34 +29,85 @@ namespace CarManagerV3
|
||||
lblID.Text = $"ID: {car.Id}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a car property safely by executing the update action and reverting it if an exception occurs.
|
||||
/// </summary>
|
||||
/// <param name="updateAction">The update action.</param>
|
||||
/// <param name="revertAction">The action to perform when the update fails.</param>
|
||||
private void SafeUpdate(Action updateAction, Action revertAction)
|
||||
{
|
||||
try
|
||||
{
|
||||
updateAction();
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
revertAction();
|
||||
}
|
||||
}
|
||||
|
||||
//private string ValueOrFormer(string newValue, string oldValue)
|
||||
//{
|
||||
// return string.IsNullOrWhiteSpace(newValue) ? oldValue : newValue;
|
||||
//}
|
||||
|
||||
private T ValueOrFormer<T>(T newValue, T oldValue)
|
||||
{
|
||||
if (newValue is string str)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(str) ? oldValue : newValue;
|
||||
}
|
||||
return newValue;
|
||||
}
|
||||
|
||||
private void tbxMake_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
car.Make = tbxMake.Text;
|
||||
SafeUpdate(
|
||||
() => car.Make = ValueOrFormer(tbxMake.Text, car.Make),
|
||||
() => tbxMake.Text = car.Make
|
||||
);
|
||||
}
|
||||
|
||||
private void tbxModel_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
car.Model = tbxModel.Text;
|
||||
SafeUpdate(
|
||||
() => car.Model = ValueOrFormer(tbxModel.Text, car.Model),
|
||||
() => tbxModel.Text = car.Model
|
||||
);
|
||||
}
|
||||
|
||||
private void nudYear_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
car.Year = (int)nudYear.Value;
|
||||
Console.WriteLine("Year value changed: " + nudYear.Value);
|
||||
SafeUpdate(
|
||||
() => car.Year = (int)ValueOrFormer(nudYear.Value, car.Year),
|
||||
() => nudYear.Value = car.Year
|
||||
);
|
||||
}
|
||||
|
||||
private void tbxColor_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
car.Color = tbxColor.Text;
|
||||
SafeUpdate(
|
||||
() => car.Color = ValueOrFormer(tbxColor.Text, car.Color),
|
||||
() => tbxColor.Text = car.Color
|
||||
);
|
||||
}
|
||||
|
||||
private void nudMileage_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
car.Mileage = (int)nudMileage.Value;
|
||||
SafeUpdate(
|
||||
() => car.Mileage = (int)nudMileage.Value,
|
||||
() => nudMileage.Value = car.Mileage
|
||||
);
|
||||
}
|
||||
|
||||
private void nudPrice_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
car.Price = nudPrice.Value;
|
||||
SafeUpdate(
|
||||
() => car.Price = nudPrice.Value,
|
||||
() => nudPrice.Value = car.Price
|
||||
);
|
||||
}
|
||||
|
||||
private async void btnSave_Click(object sender, EventArgs e)
|
||||
@@ -113,5 +164,22 @@ namespace CarManagerV3
|
||||
}
|
||||
return base.ProcessCmdKey(ref msg, keyData);
|
||||
}
|
||||
|
||||
private void tbxMake_Leave(object sender, EventArgs e)
|
||||
{
|
||||
tbxMake.Text = ValueOrFormer(tbxMake.Text, car.Make);
|
||||
}
|
||||
|
||||
private void tbxModel_Leave(object sender, EventArgs e)
|
||||
{
|
||||
tbxModel.Text = ValueOrFormer(tbxModel.Text, car.Model);
|
||||
}
|
||||
|
||||
private void tbxColor_Leave(object sender, EventArgs e)
|
||||
{
|
||||
tbxColor.Text = ValueOrFormer(tbxColor.Text, car.Color);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user