87 lines
2.4 KiB
C#
87 lines
2.4 KiB
C#
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 CarItem : UserControl
|
|
{
|
|
private static readonly HttpClient httpClient = new HttpClient();
|
|
|
|
public CarItem(Car carItem)
|
|
{
|
|
InitializeComponent();
|
|
|
|
NameLabel.Text = carItem.Name;
|
|
ClassLabel.Text = carItem.Class;
|
|
ModelLabel.Text = "Модель:" + carItem.Model;
|
|
ManufacturerLabel.Text = carItem.Manufacturer;
|
|
MoneyTextBox.Text = '$' + carItem.Price.ToString();
|
|
|
|
LoadImageAsync(carItem.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<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)
|
|
{
|
|
(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;
|
|
}
|
|
}
|
|
}
|
|
} |