title: Powershell: Listview - image
body: <p>i am struggling with bug hunting within my code:</p>
  
  <p><strong>Desired behavior:</strong> I will have list of calls with random parameters, for example:</p>
  
  <ol>
  <li>call: Icon("AAA") </li>
  <li>call: Icon("AAA")</li>
  <li>call: Icon("BBB")</li>
  <li>call: Icon("AAA")</li>
  <li>call: Icon("BBB")</li>
  <li>call: Icon("BBB")</li>
  </ol>
  
  <p>According called parameter, icon should be loaded and displayed in listview.</p>
  
  <p><strong>Real behavior:</strong> Displayed icon depends on last called parameter. Does it mean if last call parameter is BBB, icon bbb.ico is displayed for all listitem. If last call parameter is AAA, icon aaa.ico is displayed for all listitems.</p>
  
  <p><strong>Code:</strong></p>
  
  <p>function Icon($command){</p>
  
  <pre><code>if ($command -like '*AAA*'){
      $bitm=[System.Drawing.Image]::FromFile("c:\psn\aaa.ico") 
  }
  
  
  if ($command -like '*BBB*'){
      $bitm=[System.Drawing.Image]::FromFile("c:\psn\bbb.ico") 
  }
  
      $imageListSmall = new-Object System.Windows.Forms.ImageList
      $imageListSmall.Images.Add($bitm)
      #ListView
      $objListView.SmallImageList = $imageListSmall;
      $objListView.Items.Add($command,0)
      $bitm.Dispose #doesn't have impact on solution
  </code></pre>
  
  <p>}</p>
  
  <p>ListView is created within Form...</p>
  
  <pre><code>{
    $objListView = New-Object System.Windows.Forms.ListView
    $objListView.View = [System.Windows.Forms.View]::Details
    $objListView.FullRowSelect = $true
    $LVcol1 = New-Object System.Windows.Forms.ColumnHeader
    $objListView.Columns.AddRange(
      [System.Windows.Forms.ColumnHeader[]](@($LVcol1))) 
    $LVcol1.Text = "Events"
    $LVcol1.Width = 165
  
    $objListView.Location = New-Object System.Drawing.Size(10,40) 
    $objListView.Size = New-Object System.Drawing.Size(260,120) 
    $objListView.Height = 120
    $objListView.Add_DoubleClick({Clicked;$objListView.Items.Remove($objListView.SelectedItems[0])})
    $objForm.Controls.Add($objListView) 
  }
  </code></pre>
  
  <p>Update of ListView is called through timer called in regular intervals where "update" is checked regularly. Icon will change when last item is changed from AAA to BBB. But it change all icons not only required.</p>
  
  <p>Any idea where could be the issue?</p>