using System; using System.Drawing; using System.IO; using System.Net.Http; using System.Threading.Tasks; using System.Windows.Forms; using GtaVUsersInfo.Helpers; namespace GtaVUsersInfo.Controls { public partial class CarControl : UserControl { private static readonly HttpClient httpClient = new HttpClient(); private Car car; public Car Car { get { return car; } set { car = value; } } public ContextMenuStrip MenuStrip { get { return ContextMenuStrip; } set { ContextMenuStrip = value; } } public CarControl(Car carItem) { InitializeComponent(); Car = carItem; NameLabel.Text = Car.Name; ManufacturerLabel.Text = Car.Manufacturer; ClassTextBox.Text = Car.Class; ModelTextBox.Text = Car.Model; MoneyTextBox.Text = '$' + Car.Price.ToString(); LoadImageAsync(Car.Photo); } public void EditCar(Car car) { Car = car; NameLabel.Text = Car.Name; ManufacturerLabel.Text = Car.Manufacturer; ClassTextBox.Text = Car.Class; ModelTextBox.Text = Car.Model; MoneyTextBox.Text = '$' + Car.Price.ToString(); LoadImageAsync(Car.Photo); } private async void LoadImageAsync(string imageUrl) { try { byte[] imageBytes = await httpClient.GetByteArrayAsync(imageUrl); using (var ms = new MemoryStream(imageBytes)) { var bitmap = new Bitmap(ms); SetImage(bitmap); } } catch (HttpRequestException ex) { ErrorsShow.ShowErrorMessage($"Ошибка HTTP: {ex.Message}"); } catch (Exception ex) { ErrorsShow.ShowErrorMessage($"Произошла неожиданная ошибка: {ex.Message}"); } } private void SetImage(Image image) { if (InvokeRequired) { Invoke(new Action(SetImage), image); } else { PhotoBox.Image = image; } } 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) { Car.Price = int.Parse((sender as TextBox).Text); (sender as TextBox).Text = '$' + (sender as TextBox).Text; } private void carPrice_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { NameLabel.Focus(); e.Handled = true; return; } if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { e.Handled = true; } } } }