2024-10-08 09:53:24 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using GtaVUsersInfo.Helpers;
|
2024-10-10 13:47:29 +03:00
|
|
|
|
using GtaVUsersInfo.Sources;
|
2024-10-08 09:53:24 +03:00
|
|
|
|
|
|
|
|
|
namespace GtaVUsersInfo.Controls
|
|
|
|
|
{
|
2024-10-08 15:14:10 +03:00
|
|
|
|
public partial class CarControl : UserControl
|
2024-10-08 09:53:24 +03:00
|
|
|
|
{
|
|
|
|
|
private static readonly HttpClient httpClient = new HttpClient();
|
2024-10-08 14:41:32 +03:00
|
|
|
|
private Car car;
|
2024-10-10 13:47:29 +03:00
|
|
|
|
|
2024-10-08 14:41:32 +03:00
|
|
|
|
public Car Car
|
|
|
|
|
{
|
|
|
|
|
get { return car; }
|
|
|
|
|
set { car = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ContextMenuStrip MenuStrip
|
|
|
|
|
{
|
|
|
|
|
get { return ContextMenuStrip; }
|
|
|
|
|
set { ContextMenuStrip = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 15:14:10 +03:00
|
|
|
|
public CarControl(Car carItem)
|
2024-10-08 09:53:24 +03:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
2024-10-08 14:41:32 +03:00
|
|
|
|
Car = carItem;
|
|
|
|
|
|
|
|
|
|
NameLabel.Text = Car.Name;
|
|
|
|
|
ManufacturerLabel.Text = Car.Manufacturer;
|
|
|
|
|
ClassTextBox.Text = Car.Class;
|
|
|
|
|
ModelTextBox.Text = Car.Model;
|
|
|
|
|
MoneyTextBox.Text = '$' + Car.Price.ToString();
|
|
|
|
|
|
2024-10-10 13:47:29 +03:00
|
|
|
|
ChangeColor();
|
|
|
|
|
|
2024-10-08 14:41:32 +03:00
|
|
|
|
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();
|
2024-10-08 09:53:24 +03:00
|
|
|
|
|
2024-10-08 14:41:32 +03:00
|
|
|
|
LoadImageAsync(Car.Photo);
|
2024-10-08 09:53:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<Image>(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)
|
|
|
|
|
{
|
2024-10-08 14:41:32 +03:00
|
|
|
|
Car.Price = int.Parse((sender as TextBox).Text);
|
2024-10-08 09:53:24 +03:00
|
|
|
|
(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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-10 13:47:29 +03:00
|
|
|
|
|
|
|
|
|
private void ChangeColor()
|
|
|
|
|
{
|
|
|
|
|
this.BackColor = car.color;
|
|
|
|
|
MoneyTextBox.BackColor = car.color;
|
|
|
|
|
ClassTextBox.BackColor = car.color;
|
|
|
|
|
ModelTextBox.BackColor = car.color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeColor(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
using (ColorDialog colorDialog = new ColorDialog())
|
|
|
|
|
{
|
|
|
|
|
if (colorDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
car.color = colorDialog.Color;
|
|
|
|
|
ChangeColor();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-08 09:53:24 +03:00
|
|
|
|
}
|
|
|
|
|
}
|