如何删除列表框中的所选项目 [英] how to delete selected item in listbox

查看:336
本文介绍了如何删除列表框中的所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void DeleteSelectedList(ListBox LstDelete,string path)
        {
            int i = 0;
            ftp1 = new FTPOperations();
            int s = LstDelete.Items.Count;
            for (i = 0; i< LstDelete.Items.Count; i++)
            {
                if (LstDelete.Items[i].Selected)
                {
                    ftp1.DeleteFTP(path + LstDelete.Items[i].Text);
                    LstDelete.Items.Remove(LstDelete.Items[i].Text);

                }
            }

        }


我在列表框中选择了4个项目,但它仅删除了列表框中的3个项目,可以检查上面的代码是否正确..请...


i have selected 4 items in the list box but it deleting only 3 items in the list box can check the above code is correct are not..please...

推荐答案



像这样尝试..



try like this,..

public void DeleteSelectedList(ListBox LstDelete,string path)
        {
            int i = 0;
            ftp1 = new FTPOperations();
            int s = LstDelete.Items.Count;
            for (i = 0; i< LstDelete.Items.Count; i++)
            {
                if (LstDelete.Items[i].Selected)
                {
                    ftp1.DeleteFTP(path + LstDelete.Items[i].Text);
                    LstDelete.Items.RemoveAt(i);
                    i--;
                }
                
            }

        }



希望它能工作



hope it works


我已经在列表框中选择了4个项目,但是它仅删除了列表框中的3个项目,可以检查上面的代码是否正确.
可能是这种情况,因为您正在循环从列表中删除项目.

为了获得最佳/确定的结果,我建议您跟踪要删除的ID.有了列表后,请删除所有列表.

现在,尝试:
i have selected 4 items in the list box but it deleting only 3 items in the list box can check the above code is correct are not..
This might be the case as you are deleting items from the list at the same time looping through it.

For best/confirm result, I would suggest you to keep a track of ID''s to be removed. Once you have a list then remove all of them.

For now, try:
for (i = 0; i< LstDelete.Items.Count; i++)
{
     if (LstDelete.Items[i].Selected)
     {
        ftp1.DeleteFTP(path + LstDelete.Items[i].Text);
        LstDelete.Items.RemoveAt(i);
        --i;  //This will reset your counter back to same position and let item moved up the list get checked which is getting missed in your code
     }
}


这篇关于如何删除列表框中的所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆