
- VBForums
- .NET and More
- C#
- 2.0 why not remove the ghost image with this.listView1.Controls.Add(pictures) statement ?
-
May 25th, 2023, 11:21 PM
#1
Thread Starter
Hyperactive Member
why not remove the ghost image with this.listView1.Controls.Add(pictures) statement ?
in c#, when i add item to listView1 there will be two images via command:
Code:
...
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
Code:
...
PictureBox pic = (PictureBox)listView1.SelectedItems[0];
listView1.Controls.Remove(pic);
listView1.SelectedItems[0].Remove();
-
May 25th, 2023, 11:54 PM
#2
Re: why not remove the ghost image with this.listView1.Controls.Add(pictures) stateme
How could you ever get a PictureBox from the Selecteditems collection? That collection contains ListViewItems. The only place the PictureBox exists is in the Controls collection. You need to either set the Name of the PictureBox and get it from the Controls collection by name, or else store it in a separate collection, which might be a Dictionary where the ListViewItems are the keys.
-
May 26th, 2023, 03:26 AM
#3
Thread Starter
Hyperactive Member
Re: why not remove the ghost image with this.listView1.Controls.Add(pictures) stateme
I don't know how to get the PictureBox from the Selecteditems collection, I found some code documents below, I tried the code below but it still doesn't work, so I need your professional help.
Code:
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); //not working
listView1.Items.Remove(selectedItem);
}
or
Code:
// 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);//not working
}
// Removing the selected item from the list view
selectedItem.Remove();
-
May 26th, 2023, 04:33 AM
#4
Re: why not remove the ghost image with this.listView1.Controls.Add(pictures) stateme
Did you read what you posted?
// Assuming you've stored the PictureBox control as the Tag of the ListViewItem
Did you store the PictureBox control as the Tag of the ListViewItem? Not based on the code you posted earlier. If you expect to get the control out of the Tag property, you have to put it in there in the first place.

- VBForums
- .NET and More
- C#
- 2.0 why not remove the ghost image with this.listView1.Controls.Add(pictures) statement ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|