Results 1 to 4 of 4

Thread: why not remove the ghost image with this.listView1.Controls.Add(pictures) statement ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    502

    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();

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,595

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    502

    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();

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,595

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width