in c#, when i add item to listView1 there will be two images via command:
C#
string imgName = ImageList1.Images.Keys[i].ToString();
item.ImageIndex = ImageList1.Images.IndexOfKey(imgName);
listView1.Items.Add(item);
listView1.Update();
PictureBox pic = new PictureBox();
...
listView1.Controls.Add(pic);adding item to listView1 works fine but when removing item, the image is ok
added by listView1.Controls.Add statement is not deleted, I want to delete this image, how do you write the command? you see my delete code
C#
PictureBox pic = (PictureBox)listView1.SelectedItems[0];
listView1.Controls.Remove(pic);
listView1.SelectedItems[0].Remove();
Rajkiran SwainRank #165 · 14.4K reputationPosted May 26, 2023, 1:08 AM EDT
// Assuming the selected item is a ListViewItem ListViewItem selectedItem = listView1.SelectedItems[0]; // Removing the associated picture box control if (selectedItem.Tag is PictureBox pic) { listView1.Controls.Remove(pic); } // Removing the selected item from the list view selectedItem.Remove();Amit MohantyRank #112 · 20K reputationPosted May 26, 2023, 1:35 AM EDT
if (listView1.SelectedItems.Count > 0) { ListViewItem selectedItem = listView1.SelectedItems[0]; // Assuming you've stored the PictureBox control as the Tag of the ListViewItem PictureBox pic = (PictureBox)selectedItem.Tag; listView1.Controls.Remove(pic); listView1.Items.Remove(selectedItem); }Dong Lam TrienRank #1,538 · 1K reputationPosted May 26, 2023, 4:17 AM EDT
I tried running the code you sent me, all didn't work. I have debug variable item.Tag before and after running the code on variable item.Tag nothing changes, this command has no operation listView1.Controls.Remove(pic);