GtaVUsersInfo/Forms/AddCars.cs

102 lines
2.8 KiB
C#
Raw Normal View History

using GtaVUsersInfo.Helpers;
using GtaVUsersInfo.Sources;
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 GtaVUsersInfo.Forms
{
public partial class AddCars : Form
{
List<Car> carList;
Car carItem;
public AddCars(List<Car> carPrice, Car carItem)
{
InitializeComponent();
carList = carPrice;
this.carItem = carItem;
foreach (Car car in carList)
{
carComboBox.Items.Add(car.Name);
}
}
private void carComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if ((sender as ComboBox).SelectedIndex == -1)
return;
carPrice.Text = "$" + carList[(sender as ComboBox).SelectedIndex].Price.ToString();
}
private void carPrice_Enter(object sender, EventArgs e)
{
(sender as TextBox).Text = (sender as TextBox).Text.Trim('$');
}
private void carPrice_Leave(object sender, EventArgs e)
{
(sender as TextBox).Text = '$' + (sender as TextBox).Text;
}
private void carPrice_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
saveItem.Focus();
e.Handled = true;
return;
}
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
private void saveItem_Click(object sender, EventArgs e)
{
try
{
if (carComboBox.SelectedIndex == -1)
{
ErrorsShow.ShowErrorMessage("Не выбран ни один автомобиль");
return;
}
Car selectedCar = carList[carComboBox.SelectedIndex];
carItem.Name = selectedCar.Name;
carItem.Price = int.Parse(carPrice.Text.Trim('$'));
carItem.Photo = selectedCar.Photo;
carItem.Manufacturer = selectedCar.Manufacturer;
carItem.Class = selectedCar.Class;
carItem.Model = selectedCar.Model;
this.Close();
}
catch (Exception ex)
{
ErrorsShow.ShowErrorMessage($"Произошла неожиданная ошибка: {ex.Message}");
return;
}
}
public void ChangeFonts(FontSettings settings)
{
FontSettings.ChangeFontInControls(this, settings);
this.Size = new Size(this.carLabel.Width, this.Size.Height);
}
}
}