2011-08-18 106 views
0

我目前正在嘗試查看用戶在列表框中選擇的所有文件和文件夾。在這一刻,我能夠列出用戶使用openfiledialogue選擇了什麼,但是當我嘗試從列表框中刪除它時,我現在正面臨着問題。我試圖讓用戶點擊該文件旁邊的複選框,然後按下刪除鍵將其刪除如何從列表框中刪除選定的項目C#

這是我刪除按鈕

 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的財產,並有紅色的線條強調

回答

5

而不是使用listView1.SelectedItems使用listView1.CheckedItems的,改變你的button2_click到:

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

     } 
+1

不起作用。 CheckedItems不是關於Listbox的。 –

9

您的問題是因爲SelectedItems屬性實際上是對Items集合的引用,您在迭代時更改集合。 試試下面的代碼

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

BeginUpdate()EndUpdate()方法將優化這個動作的表現 - 在活動列表視圖不會刷新本身之間的這些方法調用。

+0

僅供參考,使用'ArrayList'氣餒;通用'List '類型是首選。 –

+0

@AdamRobinson如何將'listView1.SelectedItems'變成'List '? – Trev

0

試試這個

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

     } 
+1

我認爲這是完全錯誤的。 OP的刪除代碼很好 - 它正確刪除了*所有項目*,只是錯誤的 - 選中,而不是選中的項目。然而你的代碼更糟糕 - 它只會刪除所有選定項目的一部分(我猜想一半 - 假設'Remove()'是穩定的/確定性的),因爲在每次Remove()調用之後'SelectedItems'會發生變化 - 它的Count '會有所不同,項目索引會減少'1'。 – andr

0
while (listView1.SelectedItems.Count > 0) 
{ 
    listView1.Items.Remove(listView1.SelectedItems[0]); 
} 
+0

請參閱接受的答案。你們不會在這裏解決這個問題。除了OP的問題代碼與您的代碼完全相同 - 只能以相反的順序進行。 – andr

-2
$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 
+1

這將是很好的解釋爲什麼這是解決方案 –

+0

該代碼似乎不解釋如何使用它的用戶的問題。總是建議在代碼示例之前添加說明 – Prem