CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2007
    Posts
    84

    delete selected items

    hi,
    i would like to add selected items in a listbox1 to listbox2.
    after copying, i want it to be deleted from listbox1, its like moving an item and vice versa

    the problem is i cant delete the items from where it came from.
    i tried a couple of codes and the nearest is it deletes half of the selected items

    Code:
    private void bttndisAllow_Click(object sender, EventArgs e)
            {
    
    
                foreach (int i in lbAllow.SelectedIndices)
                {
                    
                    lbdisAllow.Items.Add(lbAllow.Items[i].ToString());
                }
                foreach (int i in lbAllow.SelectedIndices)
                {
                    
                    lbAllow.Items.Remove(lbAllow.SelectedItem);
                }
                
                    
                
            }
    anyone please

    tia

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: delete selected items

    Code:
          private void button1_Click(object sender, EventArgs e)
            {
                foreach (int i in lbAllow.SelectedIndices)
                {
    
                    lbdisAllow.Items.Add(listBox1.Items[i].ToString());
                }
                //This is assuming you have your ListBox's Selection Mode set to MultiExtended
                while (lbAllow.SelectedItems.Count > 0)
                {
                    lbAllow.Items.Remove(lbAllow.SelectedItem);
                }
     
            }

  3. #3
    Join Date
    Oct 2007
    Posts
    84

    Re: delete selected items

    nice work! i didnt think of that. how does it work?

    thanks!

  4. #4
    Join Date
    Sep 2006
    Posts
    31

    Re: delete selected items

    It checks if you have a item selected if so it removes it.
    It keeps doing this for al selected items

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

Featured