Home > Blockchain >  powershell adding values to listview
powershell adding values to listview

Time:12-18

i have created a form with the following codes:

##Fenster
$Fenster               = New-Object System.Windows.Forms.Form #Fenster Form wird erstellt
#$Fenster.WindowState = "Maximized"
$Fenster.AutoSize      = $True #Fenster wird auf Autogrösse gestellt
$Fenster.KeyPreview    = $True #Tastendrücke im Fenster können Aktionen auslösen
[System.Windows.Forms.Application]::EnableVisualStyles() #Desgine des Fenster wird umgestellt
$fenster.StartPosition = "Manual" #Startposition wird bestimmt
$fenster.Location = new-object System.Drawing.Size(350,250) #Startposition wird definiert



#   Panel für den Teil "Offene CLRAgents"
$TabPanelOCLRA           = New-Object Windows.Forms.Panel #Panel für die Tabs und Tabellen wird erstellt
#$TabPanel.dock      = "fill"
#$TabPanel.autosize = $true
$TabPanelOCLRA.Location = New-Object System.Drawing.Size(50,50)
$TabPanelOCLRA.Size = New-Object System.Drawing.Size(900,1200)
#$TabPanelOCLRA.BackColor = "red"
$Fenster.controls.Add($TabPanelOCLRA)



#   TabControl-für den Teil "Offene CLRAgents-auf dem-TabPanel TabPanelOCLRA
$TabControlOCLRA          = New-Object System.Windows.Forms.TabControl
$TabControlOCLRA.Anchor   = "Top,Bottom,Left"
#$TabControlOCLRA.Location = New-Object System.Drawing.Size(80, 200)
$TabControlOCLRA.autosize = $true
#TabControlOCLRA.Size = New-Object System.Drawing.Size(500,200)
$TabControlOCLRA.dock     = "fill"
#$TabControlOCLRA.BackColor = "Black"
#   Die folgende Font-Einstellung gilt für alle Steuerelemente, die mit diesem Tabcontrol verknüpft sind. 
$TabControlOCLRA.Font     = New-Object System.Drawing.Font("arial",15, [System.Drawing.FontStyle]::bold)
<#$TabControlOCLRA.add_selectedindexChanged({
    if($TabControlOCLRA.SelectedIndex -eq 0) {
        $BTKillHandles.Enabled = $false
    }
    else {
        $BTKillHandles.Enabled = $True
    }
}) #>
$TabPanelOCLRA.Controls.Add($TabControlOCLRA)



#   Tabpage für den Teil "Gekillte CLRAgents-auf-dem-TabControl TabControlOCLRA
$TabPageOCLRA            = New-Object System.Windows.Forms.TabPage
$TabPageOCLRA.autosize   = $true
$TabPageOCLRA.anchor     = "Top,Bottom,Left"
$TabPageOCLRA.Text       = "Offene CLRAgents"
$TabPageOCLRA.Font       = New-Object System.Drawing.Font("arial",14, [System.Drawing.FontStyle]::Regular)
$TabPageOCLRA.UseVisualStyleBackColor = $True
#   Listview wird dieser Seite zugefügt.
$TabPageOCLRA.controls.Add($global:ListviewOCLRA)
$TabControlOCLRA.controls.Add($TabPageOCLRA)



#   Spalten für ListView
$Spalten                      =@('Process','Process ID')
#   Globale Variable für ListView der Tabpage TabPageOCLRA
$global:ListviewOCLRA               = New-Object System.Windows.Forms.ListView
$global:ListviewOCLRA.Clear()
$global:ListviewOCLRA.View          = 'Details'
$global:ListviewOCLRA.Top           = 20
$global:ListviewOCLRA.autosize      = $true
#   Ganze Zeile markieren
$global:ListviewOCLRA.FullRowSelect = $true 
$global:ListviewOCLRA.Anchor        = "Top,Bottom,Left"
$global:ListviewOCLRA.dock          = "fill"
#$global:ListviewOCLRA.Font          = New-Object System.Drawing.Font("arial",20)
$global:ListviewOCLRA.scrollbars
$global:ListviewOCLRA.Add_Click({ 
    
})

#   Spalten der globalen Variable Listview in die Tabpage Offene CLRAgents hinzufügen 
foreach ($Spalte in $Spalten){ 
    [void]$global:ListviewOCLRA.Columns.Add($Spalte,-2)
}

........

$Fenster.ShowDialog()

I am now tring to add values to the zwei colums i have created using the variable "$spalte". I have two global arrays that have the values to be added.

$Global:addto = @("1","2","3","4","5")
$global:toadd = @("hallo", "there", "i", "am", "nothing")

i want to add the values $global:addto to the column "ProcessID" and the $global:toadd to the colum "Process". The first line of $global:addto column has the processid of the process in the first line of the column $global:toadd. How can i add them together?

I tried the following, but it adds process to the process id column and vise versa, and also it only adds the first line from the two global arrays. all the other lines can't be add.

$Additems = New-Object System.Windows.Forms.ListViewItem($Global:addto)
foreach($to in $toadd){
    [void]$Additems.SubItems.Add($to)
}
$global:ListviewOCLRA.Items.AddRange($Additems)

If you have any other methods, how this can be done with listview or else. I would love to get to konw.

CodePudding user response:

Your code could do with variable names that are more descriptive, like $addto --> $processId and $toadd --> $processName for instance. Now, you are making code where it is very easy to make mistakes..

Also, I do not see the need to scope almost every variable you have as $global:. If anywhere, you should use $script: scope inside scriptblocks that otherwise do not have any knowledge of these variables defined outside the scriptblock..

Anyway, to add values to the two columns in your ListView, you ca do this:

$addto = "1","2","3","4","5"                          # Process ID
$toadd = "hallo", "there", "i", "am", "nothing"       # Process Name

for ($i = 0; $i -lt $toadd.Count; $i  ) {
    $lvi = [System.Windows.Forms.ListViewItem]::new()
    $lvi.Text = $toadd[$i]
    $lvi.SubItems.Add($addto[$i])
    $ListviewOCLRA.Items.Add($lvi)
}

Mind you, this only works if both arrays have the same number of items.

Also, Why are these two separate arrays anyway? Now it is unsure if the ProcessId actually belongs to that Process Name.

You do not say how you created those arrays, but I gather this was done using Get-Process. In that case, you would be better off keeping the Id and Name together in one single object array you can get using

$allProcesses = Get-Process | Select-Object Id, Name

Then you can fill your listview like this:

for ($i = 0; $i -lt $allProcesses.Count; $i  ) {
    $lvi = [System.Windows.Forms.ListViewItem]::new()
    $lvi.Text = $allProcesses[$i].Name
    $lvi.SubItems.Add($allProcesses[$i].Id)
    $ListviewOCLRA.Items.Add($lvi)
}
  • Related