Question
Tableau et ListBox
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 68
il y a 17 ans 2 jours #4379
par Laurent Dardenne
Tutoriels PowerShell
Réponse de Laurent Dardenne sur le sujet Re:Tableau et ListBox
Tu peux t'inspirer de l'exemple suivant qui utilise le databinding de .NET. Cela permet de lier une collection à un composant hébergeant une liste et d'éviter une duplication des données (liste d'origine et liste du composant)
[code:1]
################################################################################
#
# Nom : TestDataBinding.ps1
# Version : 0.1
# Auteur :
# Date : le 18/03/2009
@\"
Historique :
(Soit substitution CVS)
$Log$
(soit substitution SVN)
$LastChangedDate$
$Rev$
\"@>$Null
#
################################################################################
#Création du tableau d'objet
$ArrayList= new-object System.Collections.ArrayList
$Global:Number=0
function CreateItem {
$Global:Number++
$Item = new-object PSObject
#Affichage des infos
$Item=$Item | Add-Member -membertype Noteproperty `
-name Name `
-value \"Nom$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name Description `
-value \"description$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name IP_Address `
-value \"IPAddresse$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name Passerelle `
-value \"GW$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name DNS_Prim `
-value \"DNS1-$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name DNS_Sec `
-value \"DNS2-$Number\" -pass
#etc
$ArrayList.Add($Item)
}
# crée 5 éléments
1..5|% {CreateItem}
# 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($Sender,$e){
$Form1.Close()
}
$BtnClose.Add_Click( { OnClick_BtnClose $BtnClose $EventArgs} )
#
# BtnAddItem
#
$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($Sender,$e){
CreateItem
#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 : msdn.microsoft.com/en-us/library/w67sdsex.aspx
$lstBoxObjects.DataSource = $Null
$lstBoxObjects.DataSource = $ArrayList
$lstBoxObjects.DisplayMember = \"Name\";
$lstBoxObjects.Refresh()
}
$BtnAddItem.Add_Click( { OnClick_BtnAddItem $BtnAddItem $EventArgs} )
#
# 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 = $ArrayList
#On visualise la propriété Name
$lstBoxObjects.DisplayMember = \"Name\";
#
$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($Sender,$e){
# $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 $EventArgs} )
$Form1.Add_Shown({$Form1.Activate()})
$Form1.ShowDialog()
#Libération des ressources
$Form1.Dispose()
[/code:1]
[code:1]
################################################################################
#
# Nom : TestDataBinding.ps1
# Version : 0.1
# Auteur :
# Date : le 18/03/2009
@\"
Historique :
(Soit substitution CVS)
$Log$
(soit substitution SVN)
$LastChangedDate$
$Rev$
\"@>$Null
#
################################################################################
#Création du tableau d'objet
$ArrayList= new-object System.Collections.ArrayList
$Global:Number=0
function CreateItem {
$Global:Number++
$Item = new-object PSObject
#Affichage des infos
$Item=$Item | Add-Member -membertype Noteproperty `
-name Name `
-value \"Nom$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name Description `
-value \"description$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name IP_Address `
-value \"IPAddresse$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name Passerelle `
-value \"GW$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name DNS_Prim `
-value \"DNS1-$Number\" -pass|`
Add-Member -membertype Noteproperty `
-name DNS_Sec `
-value \"DNS2-$Number\" -pass
#etc
$ArrayList.Add($Item)
}
# crée 5 éléments
1..5|% {CreateItem}
# 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($Sender,$e){
$Form1.Close()
}
$BtnClose.Add_Click( { OnClick_BtnClose $BtnClose $EventArgs} )
#
# BtnAddItem
#
$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($Sender,$e){
CreateItem
#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 : msdn.microsoft.com/en-us/library/w67sdsex.aspx
$lstBoxObjects.DataSource = $Null
$lstBoxObjects.DataSource = $ArrayList
$lstBoxObjects.DisplayMember = \"Name\";
$lstBoxObjects.Refresh()
}
$BtnAddItem.Add_Click( { OnClick_BtnAddItem $BtnAddItem $EventArgs} )
#
# 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 = $ArrayList
#On visualise la propriété Name
$lstBoxObjects.DisplayMember = \"Name\";
#
$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($Sender,$e){
# $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 $EventArgs} )
$Form1.Add_Shown({$Form1.Activate()})
$Form1.ShowDialog()
#Libération des ressources
$Form1.Dispose()
[/code:1]
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- Johan
- Auteur du sujet
- Hors Ligne
- Membre senior
-
Réduire
Plus d'informations
- Messages : 57
- Remerciements reçus 0
il y a 17 ans 2 jours #4380
par Johan
Réponse de Johan sur le sujet Re:Tableau et ListBox
Merci ca marche
et si l'on veux afficher plusieur collones ?
je ne trouve pas la syntaxe pour les DisplayMember, il ne m'affiche toujours que le dernier
et si l'on veux afficher plusieur collones ?
je ne trouve pas la syntaxe pour les DisplayMember, il ne m'affiche toujours que le dernier
Connexion ou Créer un compte pour participer à la conversation.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 68
il y a 17 ans 2 jours #4381
par Laurent Dardenne
Tutoriels PowerShell
Réponse de Laurent Dardenne sur le sujet Re:Tableau et ListBox
VashQuiRit écrit:
VashQuiRit écrit:
Dans ce cas, Il faut choisir le composant le plus adapté, le DataViewGrid. Mais du coup ta structure de donnée devra peut être évolué...et si l'on veux afficher plusieur collones ?
VashQuiRit écrit:
Je ne comprend pas, le dernier quoi ?je ne trouve pas la syntaxe pour les DisplayMember, il ne m'affiche toujours que le dernier
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- Johan
- Auteur du sujet
- Hors Ligne
- Membre senior
-
Réduire
Plus d'informations
- Messages : 57
- Remerciements reçus 0
il y a 17 ans 2 jours #4382
par Johan
Réponse de Johan sur le sujet Re:Tableau et ListBox
enregistrement (quand je meter nom et ensuite description, il n'afficher que la description)
j'ai vu DataGriedView mais je ne sais pas non plus comment le remplir XD
j'ai regardé cet exemple : powershell-scripting.com/index.php?optio...438&catid=5#3438
si je comprend bien il ya juste a rajouter $dataGridView1.DataSource = $table ?
dois-je créer les collones au préalable ? (car rien ne s'affiche)
EDIT : C'est bon ca marche, c'ets le même mecanisme que pour la listbox<br><br>Message édité par: VashQuiRit, à: 19/03/09 10:05
j'ai vu DataGriedView mais je ne sais pas non plus comment le remplir XD
j'ai regardé cet exemple : powershell-scripting.com/index.php?optio...438&catid=5#3438
si je comprend bien il ya juste a rajouter $dataGridView1.DataSource = $table ?
dois-je créer les collones au préalable ? (car rien ne s'affiche)
EDIT : C'est bon ca marche, c'ets le même mecanisme que pour la listbox<br><br>Message édité par: VashQuiRit, à: 19/03/09 10:05
Connexion ou Créer un compte pour participer à la conversation.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 68
il y a 17 ans 2 jours #4384
par Laurent Dardenne
Tutoriels PowerShell
Réponse de Laurent Dardenne sur le sujet Re:Tableau et ListBox
Un autre exemple,
Content Filtering System Whitelist GUI for Exchange 2007
powershell.com/cs/media/p/1698.aspx
Content Filtering System Whitelist GUI for Exchange 2007
powershell.com/cs/media/p/1698.aspx
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- Johan
- Auteur du sujet
- Hors Ligne
- Membre senior
-
Réduire
Plus d'informations
- Messages : 57
- Remerciements reçus 0
il y a 17 ans 2 jours #4385
par Johan
Réponse de Johan sur le sujet Re:Tableau et ListBox
Un nouveau problème ce pose a moi maintenant
Avec mon ancienne listbox je recuperé la ligne séléctioné pour faire des opérations.
Avec la datagridview je n'arrive pas a recuperé les informations de la ligne séléctioné, comment faire ?
Avec mon ancienne listbox je recuperé la ligne séléctioné pour faire des opérations.
Avec la datagridview je n'arrive pas a recuperé les informations de la ligne séléctioné, comment faire ?
Connexion ou Créer un compte pour participer à la conversation.
Temps de génération de la page : 0.105 secondes
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- Tableau et ListBox