Multiitem deleting from the list box plz

Last post 08-10-2007 5:53 PM by dilip12. 8 replies.

Sort Posts:

  • Multiitem deleting from the list box plz

    08-05-2007, 10:19 PM
    • Loading...
    • dilip12
    • Joined on 04-25-2007, 10:16 PM
    • Posts 177

     Hi i have a list box adding items to this list box from another. i dont have problem in adding multiple items from one list box to another. i have problem if i delete multiple items from the list box. i have a list box called listbxselComp and if i click on button it has to remove the selected items from the list box. when i am doing so its good if i delete for 5 items. if i add say some 20 items its not deleting if i select all items to be deleted at once. plz could you let me know any logic problem in the below code plz

    for
    (i= listbxSelComp.Items.Count - 1; i>= 0; i--)

    {       if (listbxSelComp.Items[i].Selected==true)

    {

                               listbxSelComp.Items.Remove(listbxSelComp.Items[i]);

    }

    }

     

    Thanks in advance

  • Re: Multiitem deleting from the list box plz

    08-06-2007, 1:09 AM
    • Loading...
    • shaikhather
    • Joined on 06-30-2003, 2:35 AM
    • Abu Dhabi, UAE
    • Posts 159

    instead of using index property use

     

    foreach (ListItem lItem in listbxSelComp.Items)
    {
    if (lItem.selected == true)
      listbxSelComp.Items.Remove(lItem)
    }
    
     

     

    Regards,

    Ather Ali Shaikh
    Sr. System Analyst
    GASCO
    Abu Dhabi, UAE.
  • Re: Multiitem deleting from the list box plz

    08-06-2007, 1:23 AM
    • Loading...
    • dilip12
    • Joined on 04-25-2007, 10:16 PM
    • Posts 177

    Thanks for your reply sir. i have changed in my exisging code but i got error with the following :

    error: collection was modified; enumeration operation may not execute.

    at line:  foreach (ListItem lItem in listbxSelComp.Items)

    could you help me in this regards plz thks

     

  • Re: Multiitem deleting from the list box plz

    08-06-2007, 1:41 AM
    • Loading...
    • shaikhather
    • Joined on 06-30-2003, 2:35 AM
    • Abu Dhabi, UAE
    • Posts 159

    then you have to looping backward.

     

    Dim mIndex As Integer
    For mIndex = listbxSelComp.Items.Count - 1 to 0 ' Looping Backwards
    If listbxSelComp.Items(mIndex ).Selected Then
    listbxSelComp.Items.Remove(listbxSelComp.Items(mIndex )
    End If
    Next

     I am sure it will work.

    Regards,

     

    Ather Ali Shaikh
    Sr. System Analyst
    GASCO
    Abu Dhabi, UAE.
  • Re: Multiitem deleting from the list box plz

    08-06-2007, 2:16 AM
    Answer

    Try to get the selected items in collections then sort the collection and then remove list items using negative index.

    Hope the following sample code helps.

    List<int> selection = new List<int>();for (int i = 0; i < ListBox1.Items.Count; i++)

    {

    if (ListBox1.Items[i].Selected == true)

    {

    selection.Add(i);

    }

    }

    selection.Sort();

    for (int i = 0; i < selection.Count; i++)

    {

    ListBox1.Items.RemoveAt(selection[i] - i);

    }

  • Re: Multiitem deleting from the list box plz

    08-08-2007, 12:30 AM
    • Loading...
    • shaikhather
    • Joined on 06-30-2003, 2:35 AM
    • Abu Dhabi, UAE
    • Posts 159

    If you find that my posting is your required answer, please click the Answer on my posting.

    Regards,

    Ather Ali Shaikh
    Sr. System Analyst
    GASCO
    Abu Dhabi, UAE.
  • Re: Multiitem deleting from the list box plz

    08-08-2007, 2:07 AM
    • Loading...
    • dilip12
    • Joined on 04-25-2007, 10:16 PM
    • Posts 177

    This is how i changed. but no change. i dont know why 

     

    for
    (CountSelComp = listbxSelComp.Items.Count - 1; CountSelComp >= 0; CountSelComp--)

    {

    if (listbxSelComp.Items[CountSelComp].Selected==true)

    {

    listbxSelComp.Items.Remove(listbxSelComp.Items[CountSelComp]);

    }

    }

     

    Thks

    Dk

  • Re: Multiitem deleting from the list box plz

    08-09-2007, 5:20 AM
    Answer

    Hi Dk,

    Please try the following code

     protected void Button1_Click(object sender, EventArgs e)
        {
            ArrayList values=new ArrayList ();
            foreach (ListItem li in ListBox1.Items)
            {
                if (!li.Selected)
                    values.Add(li.Text);
            }
            ListBox1.DataSource = values;
            ListBox1.DataBind();
        }

    Hope it helps,

    Jessica

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Multiitem deleting from the list box plz

    08-10-2007, 5:53 PM
    • Loading...
    • dilip12
    • Joined on 04-25-2007, 10:16 PM
    • Posts 177

     

    Thanks you sir its fixed

     

Page 1 of 1 (9 items)
Microsoft Communities
Page view counter