【问题标题】:How to Remove selected item from listbox C#如何从列表框中删除选定的项目C#
【发布时间】:2011-08-18 05:13:47
【问题描述】:

我目前正在尝试查看用户在列表框中选择的所有文件和文件夹。目前,我可以使用 openfiledialogue 列出用户选择的内容,但是当我尝试将其从列表框中删除时,我现在面临的问题。我试图让用户单击文件旁边的复选框,然后按删除按钮将其删除

这是我删除按钮的代码

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

    }

这是添加文件到列表框以供参考,以防万一

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

    }

我按下删除按钮没有任何反应,我在谷歌上看到了一些关于使用 selectionmode 的答案,但是当我使用它时,我的列表框没有 selectionmode 的属性,并且有红线下划线

【问题讨论】:

    标签: c# winforms listbox


    【解决方案1】:

    您的问题是因为 SelectedItems 属性实际上是对 Items 集合的引用,并且您在迭代它时更改了集合。 试试下面的代码

    listView1.BeginUpdate();
    ArrayList vSelectedItems = new ArrayList(listView1.SelectedItems);
    foreach (string item in vSelectedItems)
    {
       listView1.Items.Remove(item);
    }
    listView1.EndUpdate();
    

    BeginUpdate()EndUpdate() 方法将优化此操作的性能 - 在这些方法调用之间的活动期间,listView 不会自行刷新。

    【讨论】:

    • 仅供参考,不鼓励使用ArrayList;首选通用 List<T> 类型。
    • @AdamRobinson 你如何将listView1.SelectedItems 变成List<T>
    【解决方案2】:

    不要使用listView1.SelectedItems 而是使用listView1.CheckedItems 并将您的button2_click 更改为:

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

    【讨论】:

      【解决方案3】:

      试试这个

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

      【讨论】:

      • 我认为这是完全错误的。 OP 的删除代码很好 - 它正确删除了 所有项目,只是错误的项目 - 被选中而不是选中的项目。但是,您的代码更糟-它只会删除所有选定项目的一部分(我猜一半-假设Remove()是稳定的/确定性的),因为SelectedItems会在每次Remove()调用后发生变异-它的Count会有所不同,并且项目索引将减少1
      【解决方案4】:
      while (listView1.SelectedItems.Count > 0)
      {
          listView1.Items.Remove(listView1.SelectedItems[0]);
      }
      

      【讨论】:

      • 请查看已接受的答案。你的不会在这里解决问题。除了问题中的 OP 代码与您的代码相同 - 只是以相反的顺序。
      【解决方案5】:
      $x = @()
      
      [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
      [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
      
      $objForm = New-Object System.Windows.Forms.Form 
      $objForm.Text = "hello first"
      $objForm.Size = New-Object System.Drawing.Size(600,600) 
      $objForm.StartPosition = "CenterScreen"
      
      $objForm.KeyPreview = $True
      
      $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
          {
              foreach ($objItem in $objListbox.SelectedItems)
                  {$x += $objItem}
              $objForm.Close()
          }
          })
      
      $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
          {$objForm.Close()}})
      
      $OKButton = New-Object System.Windows.Forms.Button
      $OKButton.Location = New-Object System.Drawing.Size(10,250)
      $OKButton.Size = New-Object System.Drawing.Size(85,30)
      $OKButton.Text = "Start Check"
      
      $MoveRight = New-Object System.Windows.Forms.Button
      $MoveRight.Location = New-Object System.Drawing.Size(200,80)
      $MoveRight.Size = New-Object System.Drawing.Size(70,20)
      $MoveRight.Text = ">>"
      
      $objForm.Controls.Add($MoveRight)
      
      
      $MoveRight.Add_Click(
         {
              foreach ($objItem in $objListbox.SelectedItems)
                  {[void] $objListbox2.Items.Add($objItem)}
      
      
                  $selectedItems = $objListbox.SelectedItems;
      
      if ($objListbox.SelectedIndex -ne -1)
      { 
          for ($i = $selectedItems.Count - 1; $i -ge 0; $i--){$objListbox.Items.Remove($SelectedItems[$i])}
      }
      })
      
      $MoveLeft = New-Object System.Windows.Forms.Button
      $MoveLeft.Location = New-Object System.Drawing.Size(200,120)
      $MoveLeft.Size = New-Object System.Drawing.Size(70,20)
      $MoveLeft.Text = "<<"
      
      $objForm.Controls.Add($MoveLeft)
      
      $MoveLeft.Add_Click(
         {
      
              foreach ($objItem in $objListbox2.SelectedItems)
                  {[void] $objListbox.Items.Add($objItem)
              }
      
      
                  $selectedItems = $objListbox2.SelectedItems;
      
      if ($objListbox2.SelectedIndex -ne -1)
      { 
          for ($i = $selectedItems.Count - 1; $i -ge 0; $i--){$objListbox2.Items.Remove($SelectedItems[$i])}
      }
      
      })
      
      $Clearconsole = New-Object System.Windows.Forms.Button
      $Clearconsole.Location = New-Object System.Drawing.Size(505,320)
      $Clearconsole.Size = New-Object System.Drawing.Size(70,20)
      $Clearconsole.Text = "Clr console"
      
      $objForm.Controls.Add($Clearconsole)
      
      $Clearconsole.Add_Click(
         {
           $outputBox.Clear()
      
         })
      
      $MoveAll = New-Object System.Windows.Forms.Button
      $MoveAll.Location = New-Object System.Drawing.Size(200,160)
      $MoveAll.Size = New-Object System.Drawing.Size(70,20)
      $MoveAll.Text = "ALL"
      
      $objForm.Controls.Add($MoveAll)
      
      
      $MoveAll.Add_Click(
         {
              foreach ($objItem in $objListbox.Items)
                  {[void] $objListbox2.Items.Add($objItem)}
      
      
      $objListbox.Items.Clear()
          $outputBox.text="test"
         })
      
      $objLabel = New-Object System.Windows.Forms.Label
      $objLabel.Location = New-Object System.Drawing.Size(10,320) 
      $objLabel.Size = New-Object System.Drawing.Size(280,20) 
      $objLabel.Text = "Console Window"
      $objForm.Controls.Add($objLabel) 
      
      $outputBox = New-Object System.Windows.Forms.TextBox 
      $outputBox.Location = New-Object System.Drawing.Size(10,350) 
      $outputBox.Size = New-Object System.Drawing.Size(565,200) 
      $outputBox.MultiLine = $True 
      
      $outputBox.ScrollBars = "Vertical" 
      
      $objForm.Controls.Add($outputBox) 
      
      $OKButton.Add_Click(
         {
              foreach ($objItem in $objListbox.SelectedItems)
                  {$x += $objItem}
              $objForm.Close()
         })
      
      $objForm.Controls.Add($OKButton)
      
      $objLabel = New-Object System.Windows.Forms.Label
      $objLabel.Location = New-Object System.Drawing.Size(10,20) 
      $objLabel.Size = New-Object System.Drawing.Size(280,20) 
      $objLabel.Text = "Please make a selection from the list below:"
      $objForm.Controls.Add($objLabel) 
      
      $objListbox = New-Object System.Windows.Forms.Listbox 
      $objListbox.Location = New-Object System.Drawing.Size(10,40) 
      $objListbox.Size = New-Object System.Drawing.Size(160,20) 
      
      $objListbox.SelectionMode = "MultiExtended"
      
      [void] $objListbox.Items.Add("a")
      [void] $objListbox.Items.Add("b")
      [void] $objListbox.Items.Add("c")
      [void] $objListbox.Items.Add("d")
      [void] $objListbox.Items.Add("e")
      
      $objListbox.Height = 200
      $objListBox.Sorted = $True
      $objForm.Controls.Add($objListbox) 
      $objForm.Topmost = $True
      
      $objListbox2 = New-Object System.Windows.Forms.Listbox 
      $objListbox2.Location = New-Object System.Drawing.Size(315,40) 
      $objListbox2.Size = New-Object System.Drawing.Size(160,20) 
      
      $objListbox2.SelectionMode = "MultiExtended"
      
      $objListbox2.Height = 200
      $objListBox2.Sorted = $True
      $objForm.Controls.Add($objListbox2) 
      $objForm.Topmost = $True
      
      $objForm.Add_Shown({$objForm.Activate()})
      [void] $objForm.ShowDialog()
      
      $x
      

      【讨论】:

      • 最好能解释一下为什么这是解决方案
      • 代码似乎没有解释如何使用它来解决用户的问题。始终建议在代码示例之前添加说明
      猜你喜欢
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多