private HashSet<string> addedItems = new HashSet<string>();
private List<ListViewItem> addedItemsList = new List<ListViewItem>();
private TextBox textBoxEditor;
private ComboBox comboBoxEnchantment;
private ComboBox comboBoxQuality;
public Form2()
{
InitializeComponent();
this.FormClosed += (sender, e) => Application.Exit();
InitializeEditor();
InitializeComboBoxes();
InitializeImageList();
listView1.MouseDoubleClick += listView_MouseDoubleClick;
listView1.Controls.Add(textBoxEditor);
listView1.Controls.Add(comboBoxEnchantment);
listView1.Controls.Add(comboBoxQuality);
}
private void InitializeEditor()
{
textBoxEditor = new TextBox
{
Visible = false
};
textBoxEditor.Leave += (s, ea) => EndEdit();
textBoxEditor.KeyPress += TextBoxEditor_KeyPress;
}
private void InitializeComboBoxes()
{
comboBoxEnchantment = CreateComboBox(new[] { “Зачарование 1”, “Зачарование 2”, “Зачарование 3” });
comboBoxQuality = CreateComboBox(new[] { “Обычное”, “Редкое”, “Легендарное” });
}
private ComboBox CreateComboBox(string[] items)
{
var comboBox = new ComboBox
{
DropDownStyle = ComboBoxStyle.DropDownList,
Visible = false
};
comboBox.Items.AddRange(items);
comboBox.SelectedIndexChanged += ComboBoxSelectedIndexChanged;
return comboBox;
}
private void InitializeImageList()
{
string imagesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “BuyMenu”, “ItemsImage”);
LoadImages(imagesPath);
listView1.SmallImageList = imageList1;
listView2.SmallImageList = imageList1;
listView1.ListViewItemSorter = new SortList(1);
listView2.ListViewItemSorter = new SortList(1);
}
private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);
ListViewItem.ListViewSubItem subItem = hit.SubItem;
if (hit.Item != null && subItem != null)
{
if (hit.Item.SubItems.IndexOf(subItem) is var columnIndex && (columnIndex == EnchantmentColumnIndex || columnIndex == QualityColumnIndex))
{
ShowComboBox(columnIndex == EnchantmentColumnIndex ? comboBoxEnchantment : comboBoxQuality, subItem);
}
else
{
EditListViewItem(hit);
}
}
}
private void EditListViewItem(ListViewHitTestInfo hit)
{
Rectangle rect = new Rectangle(hit.SubItem.Bounds.X, hit.SubItem.Bounds.Y + 22, hit.SubItem.Bounds.Width, hit.SubItem.Bounds.Height);
textBoxEditor.SetBounds(rect.X, rect.Y, rect.Width, rect.Height);
textBoxEditor.Text = hit.SubItem.Text;
textBoxEditor.Visible = true;
textBoxEditor.Focus();
}
private void TextBoxEditor_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
EndEdit();
}
}
private void EndEdit()
{
if (listView1.SelectedItems.Count > 0)
{
var item = listView1.SelectedItems[0];
if (int.TryParse(textBoxEditor.Text, out var value))
{
item.SubItems[CountColumnIndex].Text = value.ToString();
textBoxEditor.Visible = false;
}
else
{
MessageBox.Show(“Введите числовое значение.”, “Ошибка”, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ShowComboBox(ComboBox comboBox, ListViewItem.ListViewSubItem subItem)
{
comboBox.Bounds = subItem.Bounds;
comboBox.SelectedItem = subItem.Text;
comboBox.Visible = true;
comboBox.Focus();
}
private void ComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0 && sender is ComboBox comboBox)
{
var item = listView1.SelectedItems[0];
int columnIndex = comboBox == comboBoxEnchantment ? EnchantmentColumnIndex : QualityColumnIndex;
item.SubItems[columnIndex].Text = comboBox.SelectedItem.ToString();
comboBox.Visible = false;
}
}
private void LoadImages(string folderPath)
{
var dirInfo = new DirectoryInfo(folderPath);
if (!dirInfo.Exists)
{
MessageBox.Show($“Директория с изображениями не найдена: {folderPath}”, “Ошибка”, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
foreach (var file in dirInfo.GetFiles(“*.png”))
{
try
{
var key = Path.GetFileNameWithoutExtension(file.Name);
var image = Image.FromFile(file.FullName);
imageList1.Images.Add(key, image);
}
catch (Exception ex)
{
MessageBox.Show
It's important to note that actual production code would often require more in-depth handling to ensure robustness and user-friendliness. For instance, confirming changes, allowing cancellation, and validating input. If this is not what you were asking for, or if you need a specific part of the code explained or improved upon, please provide additional details.