2016-02-18 80 views
0

我與缺陷尋找我的代碼中掙扎:PowerShell的: - 列表視圖圖像

期望的行爲:我將與隨機參數話費清單,例如:

  1. 電話:圖標( 「AAA」)
  2. 電話:圖標( 「AAA」)
  3. 電話:圖標( 「BBB」)
  4. 電話:圖標( 「AAA」)
  5. 電話:圖標( 「BBB」)
  6. 電話:圖標( 「BBB」)

根據所謂的參數,圖標應該被加載並在列表視圖中顯示。

實際行爲:顯示的圖標取決於上次調用的參數。這是否意味着如果上次呼叫參數是BBB,則會爲所有列表項顯示圖標bbb.ico。如果上次呼叫參數爲AAA,則會爲所有列表項顯示圖標aaa.ico。

代碼:

功能圖標($命令){

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 

}

的ListView的形式內創建...

{ 
    $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) 
} 

更新的ListView的是通過定時器調用的定時器在哪裏調用定期檢查「更新」。當最後一個項目從AAA更改爲BBB時,圖標將會更改。但它不僅需要更改所有圖標。

任何想法可能是問題?

+0

你的代碼沒有顯示'$ objListView'的創建位置。在添加'$ command'之前,可能需要清除它。 –

+0

ListBox是在Form(更新後的原始文章)中創建的......我將嘗試在調用update之前執行一些清理。 – xgordon

回答

0

問題已解決。

問題在於調用listItem的錯誤索引。現在一切都按預期顯示。

$global:imageListSmall = new-Object System.Windows.Forms.ImageList 
$global:bitm2=[System.Drawing.Image]::FromFile("c:\psn\AAA.ico") 
$global:bitm=[System.Drawing.Image]::FromFile("c:\psn\BBB.ico")  
$global:imageListSmall.Images.Add("AAA",$bitm) 
$global:imageListSmall.Images.Add("BBB",$bitm2) 

function Icon($command){ 

if ($command -like '*AAA*'){ 
$objListBox.SmallImageList = $imageListSmall; 
$objListBox.Items.Add($command,0) #for first image need index 0 
} 

if ($command -like '*BBB*'){ 
$objListBox.SmallImageList = $imageListSmall; 
$objListBox.Items.Add($command,1) #for second image need index 1 
}}