Question Variable dans une listbox

Plus d'informations
il y a 1 an 4 mois #32871 par Laurent Dardenne
Réponse de Laurent Dardenne sur le sujet Variable dans une listbox
>> je ne sais pas comment mettre en résolu ...
Moi non plus ;-)

Une autre approche .

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 1 an 4 mois - il y a 1 an 4 mois #32874 par Laurent Dardenne
Réponse de Laurent Dardenne sur le sujet Variable dans une listbox
Un exemple de databinding :
# TestDataBinding.ps1

#Création du tableau d'objet
$Script:ArrayList = $null
function Set-NetworkCardList {

#force la création d'un tableau
$cartes = @(Get-NetAdapter -Physical |
Select-Object -Property Name, InterfaceDescription |
Add-Member -MemberType NoteProperty -Name Label -Value $null -PassThru |
ForEach-Object {
#Construction de la propriété 'Label' en deux passes,
$_.Label = "{0} - {1}" -F $_.Name, $_.InterfaceDescription
$_
})
#Renvoi un arraylist et pas une collection d'objet
#Sinon sous Powershell l'affichage du binding est erroné...
return , [System.Collections.ArrayList]::New($Cartes)
}

$script:ArrayList = Set-NetworkCardList

# Chargement des assemblies externes
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$pnlHaut = new-object System.Windows.Forms.Panel
$pnlBas = new-object System.Windows.Forms.Panel
$BtnClose = new-object System.Windows.Forms.Button
$BtnAddItem = new-object System.Windows.Forms.Button
$lstBoxObjects = new-object System.Windows.Forms.ListBox
#
# pnlHaut
#
$pnlHaut.Controls.Add($lstBoxObjects)
$pnlHaut.Dock = [System.Windows.Forms.DockStyle]::Fill
$pnlHaut.Location = new-object System.Drawing.Point(0, 0)
$pnlHaut.Name = "pnlHaut"
$pnlHaut.Size = new-object System.Drawing.Size(566, 329)
$pnlHaut.TabIndex = 0
#
# pnlBas
#
$pnlBas.Controls.Add($BtnAddItem)
$pnlBas.Controls.Add($BtnClose)
$pnlBas.Dock = [System.Windows.Forms.DockStyle]::Bottom
$pnlBas.Location = new-object System.Drawing.Point(0, 265)
$pnlBas.Name = "pnlBas"
$pnlBas.Size = new-object System.Drawing.Size(566, 64)
$pnlBas.TabIndex = 1
#
# BtnClose
#
$BtnClose.Location = new-object System.Drawing.Point(464, 29)
$BtnClose.Name = "BtnClose"
$BtnClose.Size = new-object System.Drawing.Size(75, 23)
$BtnClose.TabIndex = 0
$BtnClose.Text = "Close"
$BtnClose.UseVisualStyleBackColor = $true
function OnClick_BtnClose {
$Form1.Close()
}

$BtnClose.Add_Click( { OnClick_BtnClose } )

#
# BtnAddItem
# note : le code d'origine permettait d'ajouter des éléments à la volée
# Pour cette exemple cette partie est inadaptée.
#
$BtnAddItem.Location = new-object System.Drawing.Point(334, 29)
$BtnAddItem.Name = "BtnAddItem"
$BtnAddItem.Size = new-object System.Drawing.Size(83, 23)
$BtnAddItem.TabIndex = 1
$BtnAddItem.Text = "Add item"
$BtnAddItem.UseVisualStyleBackColor = $true
function OnClick_BtnAddItem {
#relecture
$script:ArrayList = Set-NetworkCardList
#If you are bound to a data source that does not implement the IBindingList interface, such as an ArrayList,
#the bound control's data will not be updated when the data source is updated.
# la suite : http://msdn.microsoft.com/en-us/library/w67sdsex.aspx
$lstBoxObjects.DataSource = $Null
$lstBoxObjects.DataSource = $script:ArrayList
$lstBoxObjects.DisplayMember = "Label";
#$lstBoxObjects.Refresh()
}

$BtnAddItem.Add_Click( { OnClick_BtnAddItem } )
#
# lstBoxObjects
#
$lstBoxObjects.Dock = [System.Windows.Forms.DockStyle]::Fill
$lstBoxObjects.FormattingEnabled = $true
$lstBoxObjects.Location = new-object System.Drawing.Point(0, 0)
$lstBoxObjects.Name = "lstBoxObjects"
$lstBoxObjects.Size = new-object System.Drawing.Size(566, 329)
$lstBoxObjects.TabIndex = 0

# --BINDING --
#On lie la listbox avec le tableau $Arraylist
$lstBoxObjects.DataSource = $script:ArrayList
#On visualise la propriété label
$lstBoxObjects.DisplayMember = "Label";


#
$Form1 = new-object System.Windows.Forms.form
#
$Form1.ClientSize = new-object System.Drawing.Size(566, 329)
$Form1.Controls.Add($pnlBas)
$Form1.Controls.Add($pnlHaut)
$Form1.Name = "Form1"
$Form1.Text = "Test DataBinding"
function OnFormClosing_Form1 {
# $this est égal au paramètre sender (object)
# $_ est égal au paramètre e (eventarg)

# Déterminer la raison de la fermeture :
# if (($_).CloseReason -eq [System.Windows.Forms.CloseReason]::UserClosing)

#Autorise la fermeture
($_).Cancel = $False
}
$Form1.Add_FormClosing( { OnFormClosing_Form1 } )
$Form1.Add_Shown({ $Form1.Activate() })
$Form1.ShowDialog()
#Libération des ressources
$Form1.Dispose()

Write-Host "Liste d'item :"
$script:ArrayList | Format-Table

Tutoriels PowerShell
Dernière édition: il y a 1 an 4 mois par Laurent Dardenne. Raison: balise code

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 1 an 4 mois #32884 par Rstyle
Réponse de Rstyle sur le sujet Variable dans une listbox
Merci Laurent ;)

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.070 secondes
Propulsé par Kunena