Объедени это в одно public void ShowCountTextBox(ListViewItem selectedItem,…
- User
Объедени это в одно public void ShowCountTextBox(ListViewItem selectedItem, ListViewItem.ListViewSubItem subItemCount) { TextBox quantityTextBox = CreateOrGetCountTextBox();
code// Определите сдвиг по Y, чтобы подвинуть вниз, например на 2 пикселя int yOffset = 20; // Получаем положение ячейки внутри ListView и применяем сдвиг Rectangle subItemRect = subItemCount.Bounds; Point textBoxLocation = selectedItem.ListView.PointToScreen(new Point(subItemRect.Left, subItemRect.Top + yOffset)); textBoxLocation = Changing.PointToClient(textBoxLocation); // Устанавливаем границы TextBox quantityTextBox.SetBounds(textBoxLocation.X, textBoxLocation.Y, subItemRect.Width, subItemRect.Height - yOffset); // Уменьшаем высоту на yOffset, чтобы сохранить размер quantityTextBox.Text = subItemCount.Text; quantityTextBox.Visible = true; quantityTextBox.BringToFront(); quantityTextBox.Focus(); // Важно! Связываем TextBox с ListViewItem через Tag quantityTextBox.Tag = selectedItem; } private TextBox CreateOrGetCountTextBox() { if (CountTextBox == null || CountTextBox.IsDisposed) { CountTextBox = new TextBox(); CountTextBox.KeyPress += CountTextBox_KeyPress; // Обновлено CountTextBox.Leave += CountTextBox_Leave; Changing.Controls.Add(CountTextBox); // Добавляем контрол на форму } return CountTextBox; } private void CountTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Return) { TextBox tb = (TextBox)sender; UpdateQuantity((ListViewItem)tb.Tag, tb.Text); tb.Visible = false; // Скрыть после Enter e.Handled = true; // Предотвратить “beep” звук } else { HandleCountKeyPress(e); } } private void UpdateQuantity(ListViewItem selectedListViewItem, string newQuantity) { // Вы можете обновить Tag или SubItem, который содержит количество if (selectedListViewItem != null && selectedListViewItem.Tag is Item item) { // Примените изменения к вашему объекту Item или обновите SubItem selectedListViewItem.SubItems[2].Text = newQuantity; // Предполагаем, что это колонка с количеством } } private void CountTextBox_Leave(object sender, EventArgs e) { TextBox tb = (TextBox)sender; UpdateQuantity((ListViewItem)tb.Tag, tb.Text); // Сохранить значение перед скрытием tb.Visible = false; // Затем скрыть TextBox } - Assistant