如何从列表框中C#删除选中的项目

如何从列表框中C#删除选中的项目

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

问题描述

我目前正试图通过查看ListBox中的用户选择的所有文件和文件夹。此刻,我能够列出哪些用户已使用openfiledialogue不过,我感到现在面临的概率,当我尝试将其删除形成列表框中选择。我试图让用户点击文件旁边的复选框,然后按下删除键将其删除。



这是我的删除按钮

$ B码
$ b

 私人无效button2_Click(对象发件人,EventArgs五)
{
的for(int i = listView1.SelectedItems.Count - 1; I> = 0;我 - )
{
listView1.Items.Remove(listView1.SelectedItems [I]);
}

}

这是将文件添加到列表框中在案件的参考仅仅指刚

 私人无效的button1_Click(对象发件人,EventArgs五)
{

打开文件对话框打开文件对话框=新的OpenFileDialog();
//显示打开文件对话框
openfiledialog.InitialDirectory =C:\\;
//openfiledialog.Multiselect = TRUE;
openfiledialog.Title =锁定文件;
openfiledialog.Filter =所有文件| *。*;
openfiledialog.ShowDialog();


如果(openfiledialog.FileName!=)
{

//通过FileInfo的阵列和存储在网络$ B的新阵列移动$ b listView1.Items.Clear();
的foreach(在openfiledialog.FileNames字符串文件)
{
listView1.Items.Add(文件);
}
}

}

和我按下删除按钮没有发生,我看到的SelectionMode的使用上对谷歌的一些答案,但是当我用,我的列表框不具备的SelectionMode的财产,有红色的线条强调


解决方案
listView1.SelectedItems 使用 listView1.CheckedItems 和改变你的 button2_click 来:

 私人无效button2_Click(对象发件人,EventArgs E)
{
的foreach(ListViewItem的我在listView1.CheckedItems)
listView1.Items.Remove(I)

}


i currently trying to view all the files and folder selected by the user in a listbox. At the Moment i am able to list what the user have chosen using the openfiledialogue HOWEVER i am now facing prob when i try to remove it form the listbox. i trying to allow the user to click on the checkbox beside the file and press the remove button to remove it

this is my code for remove button

      private void button2_Click(object sender, EventArgs e)
    {
        for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
        {
            listView1.Items.Remove(listView1.SelectedItems[i]);
        }

    }

this is the add file to listbox for reference jsut in case

    private void button1_Click(object sender, EventArgs e)
    {

        OpenFileDialog openfiledialog = new OpenFileDialog();
        // Display open file dialog
        openfiledialog.InitialDirectory = "C:\\";
        //openfiledialog.Multiselect = true;
        openfiledialog.Title = "Lock File";
        openfiledialog.Filter = "All Files | *.*";
        openfiledialog.ShowDialog();


        if (openfiledialog.FileName != "")
        {

        //move through FileInfo array and store in new array of fi
            listView1.Items.Clear();
            foreach (string file in openfiledialog.FileNames)
            {
                listView1.Items.Add(file);
            }
        }

    }

and i pressed the remove button nothing happen and i saw some answer on google on the using of selectionmode but when i used that, my listbox does not have the property of selectionmode and have red lines underlined

解决方案

Instead of using listView1.SelectedItems use listView1.CheckedItems and change your button2_click to:

private void button2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem i in listView1.CheckedItems)
                listView1.Items.Remove(i);

        }

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

09-05 23:26